Home > Net >  Change the WooCommerce loop product link based on a custom field
Change the WooCommerce loop product link based on a custom field

Time:05-10

I‘m try to create a code to change the product URL on the shop loop.

Therefore I created the following code but it does not work as expected. The goal is, that we can enter a custom URL for specific products and then when a customer clicks on that product in the shop loop, it redirects to that custom URL.

This is my current version:

add_action( 'woocommerce_product_options_advanced', 'woostore_custom_product_link' );
function woostore_custom_product_link() {
    global $woocommerce, $post;
 
    echo '<div >';
  
    woocommerce_wp_text_input( 
        array( 
            'id'          => '_custom_url', 
            'label'       => __( 'Custom Page link', 'actions-pro' ), 
            'placeholder' => 'http://',
            'desc_tip'    => 'true',
            'description' => __( 'Enter the custom product page url for this product to link to from the shop page.', 'actions-pro' ) 
        )
    );
  
    echo '</div>';
}

// Save Field
add_action( 'woocommerce_process_product_meta', 'woostore_link_to_custom_product_save' );
function woostore_link_to_custom_product_save( $post_id ) {
    if ( ! empty( $_POST['_custom_url'] ) ) {
        update_post_meta( $post_id, '_custom_url', esc_attr( $_POST['_custom_url'] ) );
    } else {
        update_post_meta( $post_id, '_custom_url', esc_attr( $_POST['_custom_url'] ) );
    }
}

add_action( 'init', 'woostore_product_change_link' );
function woostore_product_change_link() {
    global $post, $product;
    if ( get_post_meta( $post->ID, '_custom_url', true ) ) {
        remove_action( 'woocommerce_before_shop_loop_item', 'woocommerce_template_loop_product_link_open', 10 );
        
        add_action( 'woocommerce_before_shop_loop_item', 'woostore_template_loop_product_link_open', 1 );
    }
}

I didn‘t get any error. Bit it‘s not working on my side. Any advice?

CodePudding user response:

Some comments/suggestions regarding your code attempt

  • Use the woocommerce_loop_product_link filter hook vs init, this way you don't have to remove/add hooks
  • To save fields you can use the woocommerce_admin_process_product_object hook, opposite the outdated woocommerce_process_product_meta hook
  • No need to use global variables

So you get:

function action_woocommerce_product_options_advanced() {  
    woocommerce_wp_text_input( 
        array( 
            'id'          => '_custom_url', 
            'label'       => __( 'Custom Page link', 'actions-pro' ), 
            'placeholder' => 'http://',
            'desc_tip'    => 'true',
            'description' => __( 'Enter the custom product page url for this product to link to from the shop page.', 'actions-pro' ) 
        )
    );
}
add_action( 'woocommerce_product_options_advanced', 'action_woocommerce_product_options_advanced' );

// Save custom field
function action_woocommerce_admin_process_product_object( $product ) {
    // Isset
    if ( isset( $_POST['_custom_url'] ) ) {        
        // Update
        $product->update_meta_data( '_custom_url', sanitize_url( $_POST['_custom_url'] ) );
    }
}
add_action( 'woocommerce_admin_process_product_object', 'action_woocommerce_admin_process_product_object', 10, 1 );

// Custom url
function filter_woocommerce_loop_product_link( $the_permalink, $product ) {
    // Get meta value
    $url = $product->get_meta( '_custom_url' );

    // NOT empty
    if ( ! empty ( $url ) ) {
        $the_permalink = $url;
    }

    return $the_permalink;
}
add_filter( 'woocommerce_loop_product_link', 'filter_woocommerce_loop_product_link', 10, 2 );
  • Related