Skip to content

Instantly share code, notes, and snippets.

@WPDevHQ
Last active December 30, 2020 16:41
Show Gist options
  • Save WPDevHQ/2a7c82fc56db3f18e2abcebf00f41200 to your computer and use it in GitHub Desktop.
Save WPDevHQ/2a7c82fc56db3f18e2abcebf00f41200 to your computer and use it in GitHub Desktop.
Replace WooCommerce product link in shop with a custom product page url
<?php
add_action( 'woocommerce_product_options_advanced', 'woostore_elementor_product_link' );
function woostore_elementor_product_link() {
global $woocommerce, $post;
echo '<div class="options_group">';
woocommerce_wp_text_input(
array(
'id' => '_elementor_url',
'label' => __( 'Elementor Page link', 'actions-pro' ),
'placeholder' => 'http://',
'desc_tip' => 'true',
'description' => __( 'Enter the custom Elementor 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_elementor_product_save' );
function woostore_link_to_elementor_product_save( $post_id ) {
if ( ! empty( $_POST['_elementor_url'] ) ) {
update_post_meta( $post_id, '_elementor_url', esc_attr( $_POST['_elementor_url'] ) );
} else {
update_post_meta( $post_id, '_elementor_url', esc_attr( $_POST['_elementor_url'] ) );
}
}
add_action( 'init', 'woostore_product_change_link' );
function woostore_product_change_link() {
global $post, $product;
if ( get_post_meta( $post->ID, '_elementor_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', 10 );
}
}
function woostore_template_loop_product_link_open() {
global $post, $product;
$post_meta_value = get_post_meta( $post->ID, '_elementor_url', true );
echo '<a href="' . esc_url( $post_meta_value ) . '" class="woocommerce-LoopProduct-link">';
}
@WPDevHQ
Copy link
Author

WPDevHQ commented Jan 3, 2017

Everything up to the save function works.

What I'm having issues with is fetching the meta values and use it to manipulate the output.

The conditional check in function woostore_product_change_link() doesn't seem to do anything.

The url returned on the products in woostore_template_loop_product_link_open() is that of the shop page rather than the one from the meta field.

@hamadabin
Copy link

hello, can i ask a question regarding this code snippet.
I tried to implement this code in my side but it didn't work. So i must implement it wrong.

where to replace the text to suit to my my website.

For example.
if i have product id '123'
product link 'www.mywebsite/product'
custome product page'www.mywebsite/customproduct link'

Can someone show me where to replace the text in the code?

im sorry if the question is to basic. I'm not a coder. I really appreciate the help and im sure it will help others as well.

@shan111111
Copy link

this is code is not working on my website, this not even replace the product page link.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment