如何在WooCommerce变体产品中回放一段PHP代码?

时间:2018-09-26 作者:Islam Mohamed

我有一点复杂的解释问题,我希望有人能理解和帮助我。

我在WooCommerce上设置了自定义字段,用于输入每个产品的特定代码(类似于SKU,称为“asin”),然后我在后端使用亚马逊API从亚马逊的产品页面获取价格(别担心,我的问题是一般性的,与API无关)。

下面是我的简单产品类型代码示例,它的工作方式与我所需要的完全一样。

<?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;

?>  
我的问题是变化产品,因为我需要在变化之间切换时更改自定义价格。

这是我用来获取变体产品类型“asin代码”的自定义产品值的代码,它工作得很好,但我需要添加与上面简单产品类似的价格,并使用当前选定变体的asin,并在上面的亚马逊API变量上使用该asin。

我从这里得到了变体产品的自定义字段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;
    }   
    ?>  
我想这里的主要问题是,这段代码在页面呈现后使用javascript获取所选变体的asin,我需要获取所选变体的asin并将其发送到php,以便我可以通过amazon API发送它以获取价格。因此,我想知道是否有任何方法可以使用第一个简单代码段上显示的price API代码,并将其用于第二个变体代码段?

提前非常感谢:)

1 个回复
SO网友:Islam Mohamed

我解决了这个问题,我缺少的是将API返回转换为字符串,下面是代码,希望它能帮助一些人。

<?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

    }   
    ?>  

结束