0

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 :)

0

I solved the issue, what i was missing is to convert the API return to string, here is the code hopefully it helps someone.

<?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 );  
    $amazon = new AmazonAPI("GOOGLE-21", "JIJIOJEFIOJEIOFEJFIO", "DJWIJDIWOJDFIJIOFJDIOWJQFOIJIOIOJWID");
    $item = $amazon->item_lookup($variations['wccaf_de_asin_var'])->get_item_data(); 
    $variations['amz_price_var_de'] = (string) $item->price;    
    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;


        ?>
<div class="buy_amz_btn_wrap" >     
{{{data.variation.amz_price_var_de}}}
</div>
<?php

    }   
    ?>  

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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