Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am importing affiliate products via XML into Woocommerce. Due to products having variations if I use the External/Affiliate product type I get repetition of product on my front end (eg. same product with different location/colour). To avoid this I am attempting to import these products as variable products, this solves the problem, except now I need to Change the add to cart to behave like an external product (i.e. a Button that links to another website).

I've created a Custom Value associated with each product called "Buy_Link", now what?

I have searched and searched, but unable to find a solution for something I thought would be relatively simple for someone with php knowledge.

I know that I should be able to retrieve the link doing something like this:

<?php echo get_post_meta($post->ID, 'Buy_Link', true); ?>

(After some help and a bit more searching) I believe I will need to place code: ID, 'Buy_Link', true); ?>

into single-product/add-to-cart/variable.php, but I am still unsure where it would go in that template.

I would like the link to replace some of the functionality of the original add to cart button.

Obviously, I am not too familiar with php code, so please be specific about how and where in the template I can put the code. Much appreciated.

share|improve this question

closed as off-topic by toscho Apr 18 at 19:47

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

3 Answers 3

I was able to find the solution. This is so simple and I can't believe I did not just do this first, but there you are (duh), I thought it was more complex than that...

My solution is below:

Copy woocommerce template to my theme to override single-product/add-to-cart/variable.php

editing variables.php, change the

<?php echo esc_url( $product->add_to_cart_url() ); ?>

to the below (replace by calling my custom field url):

<?php echo get_post_meta($post->ID, 'Buy_Link', true); ?>

Anyway, this works a treat to get the link to the buy button (and bypasses the cart altogether) but still allowing variations to be shown. Now I just need to get a custom field for EACH variation instead of only one custom field per variable product (didn't realise this limitation)! Then I can have the appropriate link for EACH variation, not just default product variation.

Hope this helps someone out there.

share|improve this answer
    
The add_to_cart_url() is also filterable. –  helgatheviking Jan 21 at 13:56

You got the exact code to display WordPress custom field. I'm not accustomed to WooCommerce, but I can say the WordPress way, and guess WooCommerce won't be different as they all are actually go with the logic of coding.

The following code is just to show (PHP echo) the field "Buy_Link", but it doesn't fetch the field from database. So before that code, you have to fetch the associated data from the database first.

<?php echo get_post_meta($post->ID, 'Buy_Link', true); ?>

In the WordPress way, we typically do that using WP_Query() or get_posts() (accidentally with query_posts()). And after that, you have to put that after the the_post() function.

But with a WP_Query, the the_post() function would be like $query->the_post(), if we go like this:

<?php
$args = array('post_type'=>'post', 'post_status'=>'publish');

$query = new WP_Query($args);

if($query->have_posts()){
   while($query->have_posts()) {
     $query->the_post(); //is actually placing the post information here ?>
     <h2><?php the_title(); ?></h2>
     <div><?php echo get_post_meta($post->ID, 'Buy_Link', true); ?></div>
   <?php
   } //endwhile
} //endif
?>

Just observe that, we are doing a new WP_Query() with some argument, and then checking that if there is any post as we queried? Then we started a PHP while loop passing the same query, and the saying, show the_post() only from that query using $query-> before. And this $query is fetching data from the database.

So, in summery, you have to put the aforementioned code of showing custom field within a database query.

share|improve this answer
    
Thanks for this - I will take more of a look. –  Samantha Jan 5 at 11:19
    
I believe I will need to place code: <?php echo get_post_meta($post->ID, 'Buy_Link', true); ?> into single-product/add-to-cart/variable.php, but I am still unsure where it would go in that template since I would like the link to replace some of the functionality of the original add to cart button. I am afraid queries and loops are all very confusing to me. (Sorry about the double comment) –  Samantha Jan 6 at 0:38
up vote 0 down vote accepted

I was able to find a solution for adding a custom field to EACH variation, in case someone is looking for this solution, there is a tutorial here (thanks Derek!):

http://designloud.com/how-to-add-affiliate-links-to-woocommerce-variations/

I spent weeks trying to find a solution for this - Hope this saves someone some time :)

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.