admin管理员组文章数量:1122832
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 /
<?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 :)
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 :)
Share Improve this question asked Sep 26, 2018 at 19:32 Islam MohamedIslam Mohamed 311 silver badge6 bronze badges1 Answer
Reset to default 0I 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
}
?>
本文标签: How to echo a PHP code into WooCommerce variation product
版权声明:本文标题:How to echo a PHP code into WooCommerce variation product? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736289602a1928339.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论