Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

181
Views
Edit woocommerce external url

I need help on editing the WC external product URL, i want to change the button type (html) into <button data-sell-product="product-id">Buy text</button> This is how my content-product.php looks : `>

/**
 * woocommerce_before_shop_loop_item_title hook.
 *
 * @hooked woocommerce_show_product_loop_sale_flash - 10
 * @hooked woocommerce_template_loop_product_thumbnail - 10
 */
do_action( 'woocommerce_before_shop_loop_item_title' );

/**
 * woocommerce_shop_loop_item_title hook.
 *
 * @hooked woocommerce_template_loop_product_title - 10
 */
do_action( 'woocommerce_shop_loop_item_title' );

/**
 * woocommerce_after_shop_loop_item_title hook.
 *
 * @hooked woocommerce_template_loop_rating - 5
 * @hooked woocommerce_template_loop_price - 10
 */
do_action( 'woocommerce_after_shop_loop_item_title' );

/**
 * woocommerce_after_shop_loop_item hook.
 *
 * @hooked woocommerce_template_loop_product_link_close - 5
 * @hooked woocommerce_template_loop_add_to_cart - 10
 */
do_action( 'woocommerce_after_shop_loop_item' );
?>

` I tried from the dashboard but i need to edit in the HTML too. So instead setting an URL from the edit product dashboard i will just need to add the product id. Can someone help me in which file i can find this to edit?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

here's my 3 part approach. First you need to add a new text input to the product metabox. Then you need to save that data. This is where you will enter the selly.gg ID. Finally, you will filter the add to cart link to convert it to a button and pass it the ID you saved in the first two steps. It should look something like this, but I didn't test it so be wary of typos.

/*
 * Add text inputs to product metabox
 *
 * @return print HTML
 * @since 1.0
 */
function so_43684081_add_to_metabox(){
    global $post;

    $product = wc_get_product( $post->ID );

    // if variable billing is enabled, continue to show options. otherwise, deprecate
    $show_billing_period_options = wc_string_to_bool( $product->get_meta( '_variable_billing' ) );

    echo '<div class="options_group show_if_external">';

        woocommerce_wp_text_input( array(
            'id' => '_selly_id',
            'label' => __( 'Selly.gg Product ID', 'your-plugin' ),
        ) );

    echo '</div>';

}
add_action( 'woocommerce_product_options_general_product_data', 'so_43684081_add_to_metabox' );




/*
 * Save extra meta info
 *
 * @param object $post
 * @return void
 */
function so_43684081_save_product_meta( $product ) {

    if ( isset( $_POST['_selly_id'] ) ) {
        $product->update_meta_data( '_selly_id', sanitize_text_field( $_POST['_selly_id'] ) );
    } else {
        $product->delete_meta_data( '_selly_id' );
    }

}
add_action( 'woocommerce_admin_process_product_object', 'so_43684081_save_product_meta' );


/*
 * Modify the external link
 *
 * @param object $post
 * @return void
 */
function so_43684081_loop_add_to_cart_link( $link, $product ) {  

    if( $product->is_type( 'external' ) ) {
        $link = sprintf( '<button data-selly-product="%s" class="%s">%s</button>',
            esc_attr( $product->get_meta( '_selly_id' ) ),
            esc_attr( isset( $class ) ? $class : 'button' ),
            esc_html( $product->add_to_cart_text() )
        );
    }
    return $link;

add_filter( 'woocommerce_loop_add_to_cart_link', 'so_43684081_loop_add_to_cart_link', 10, 2 );
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!