douqianzha6213 2018-09-26 19:28
浏览 51

如何将PHP代码回显到WooCommerce变体产品中?

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

  • 写回答

1条回答 默认 最新

  • dqaxw44567 2018-09-26 21:57
    关注

    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
    
        }   
        ?>  
    
    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?