I have a little complex to explain the problem which I hope someone can understand and help me with.
I have set custom fields on WooCommerce which I use to enter a specific code for each product (something like SKU called “asin”), Then i use that code on my backend using Amazon API to get the price from the product page on Amazon (don’t worry my question is general and not related to API).
Here is a sample of my code for the simple product type which works perfectly like what i need.
<?php
//This is the code i use to get the "asin" value from custom fields.
$de_asin = get_post_meta(get_post()->ID, "wccaf_de_asin", true );
$associate_id = "google-21";
$access_key = "N22EKJMDWJMDWIJWIJWDIWDW";
$secret_key = "48UJIFJIJFEIFJF89U8EKFIEJIUJWHUFUEFJFSIJF";
$amazon_domain = "amazon.de";
$asin = $de_asin;
// This is the code i use to get the product price from amazon website using it's api
include 'amazon-api/amazon_class.php';
$amazon = new AmazonAPI($associate_id , $access_key, $secret_key , $amazon_domain);
$item = $amazon->item_lookup($asin)->get_item_data();
echo $item->price;
?>
My problem is with variation products since I need that custom price to change when I switch between variations.
This is the code I use to get custom product value for variation product type “asin code” which works perfectly but I need to add the price similar to what I did with the simple product above and use the current selected variation’s asin and use that asin on the amazon API variable above.
I got that custom field for variation products from here
http://www.remicorson.com/woocommerce-custom-fields-for-variations/
<?php
add_filter( 'woocommerce_available_variation', 'custom_load_variation_settings_products_fields' );
function custom_load_variation_settings_products_fields( $variations ) {
// duplicate the line for each field
$variations['wccaf_de_asin_var'] = get_post_meta( $variations[ 'variation_id' ], 'wccaf_de_asin_var', true );
$variations['wccaf_uk_asin_var'] = get_post_meta( $variations[ 'variation_id' ], 'wccaf_uk_asin_var', true );
return $variations;
}
//wccf_after_variation_description is a hook i created into variation.php
add_action( 'wccf_after_variation_description', 'wcff_variation_affiliate_button', 10);
function wcff_variation_affiliate_button() {
// IP Geolocation
$country_code = $_SERVER ["HTTP_CF_IPCOUNTRY"];
$de_asin = '{{{ data.variation.wccaf_de_asin_var }}}';
$uk_asin = '{{{ data.variation.wccaf_uk_asin_var }}}';
if ($country_code=="DE") {
$asin = $de_asin;
}
if ($country_code=="GB") {
$asin = $uk_asin;
}
echo $asin;
}
?>
I guess that the main issue here is that this code gets the selected variation’s asin using javascript after the page got rendered and i need to get that selected variation’s asin and send it to php so i can send it via amazon API to get the price.
So I’m wondering if there is any way to use the price API code shown on the first simple snippet and use it on the second variation snippet?
Thanks so much in advance 🙂