可变产品选择:获取实时选择的值
在WooCommerce使用下面的代码在简单的产品价格和变量的产品后添加自定义标签:可变产品选择:获取实时选择的值
add_filter('woocommerce_variation_price_html','prices_custom_labels', 10, 2 );
add_filter('woocommerce_price_html','prices_custom_labels', 10, 2);
function prices_custom_labels($price, $product){
// Set HERE your custom labels names
$per_dozen = ' '. __('per dozen', 'woocommerce');
$per_case = ' '. __('per case (20 dozens)', 'woocommerce');
// 1) Variable products
if ($product->product_type != 'simple' && $product->variation_id) {
// Getting the array of existing attributes values for a variation
$variation_attribute_value = $product->variation_data;
// Here we keep only the last value in this array
$last_variation_attribute_slug_value = ' ' . end($variation_attribute_value);
// Finding the word 'case' in the attribute value slug
$has_case = strstr($last_variation_attribute_slug_value, 'case');
// Setting the right displayed label depending on attribute value slug
if($has_case)
$attribute_quantity_name_value = $per_case;
else
$attribute_quantity_name_value = $per_dozen;
// Here the output price + custom label
$price = '<ins class="highlight">'.woocommerce_price($product- >regular_price).$attribute_quantity_name_value.'</ins>';
}
// 2) Simple products
else
{
// Here the output price + custom default label
$price = '<ins class="highlight">'.woocommerce_price($product- >regular_price).$per_dozen.'</ins>';
}
return $price;
}
但在变型产品,我与问题在现场展示的价格中附加了自定义标签。我使用的代码只在现场价格“每打”后显示。
我将需要获得定制的“量”的选择所选择的值后的价格加入正确的标签:
- 如果选择的值是“打”我需要后,现场的价格,以显示“每打(012打)“,
- 如果选定的值是”大小写(20打)“,我需要在每个案例(20打)后以实时价格显示。
这张截图是我实际上是所有情况:
检查这个问题上我的网站specific product page
所以我需要得到属性“量“选定的值将正确的标签附加到现场价格。
任何帮助?我该怎么做才能做到这一点?
我已经尝试了很多代码,我无法得到它的工作。
得到这个工作的唯一方法是使用JavaScript/jQuery的,但它是复杂因为WooCommerce已经在其上运行了一些Javascript/Ajax代码。
首先,是不可能检测所选择的客户的选择上选择为WooCommerce从<option>
HTML标签去除"selected"
属性。
一旦顾客有构成一个完整的选择(所以选用从该变量产品一种变体中),Woocommerce在隐藏
<imput>
HTML字段添加相应的变化ID值,并显示相应的价格。我们PHP代码通的JavaScript 变化的ID的该变量的产品的阵列,与“量”属性的为每个的相应值。
然后我们可以使用“上模糊”在
<select>
html标签JavaScript事件来获取隐藏variation ID
值,然后追加与正确的“标签”的价格。
下面是功能代码,将一个自定义标签添加到根据客户选择实时价格(所以选择产品差异):
add_action('woocommerce_after_add_to_cart_form', 'custom_get_variations_js');
function custom_get_variations_js() {
global $product;
// Set HERE your "quantity" attribute slug
$attribute_qty_slug = 'pa_quantity';
$attribute_qty_slug_key = 'attribute_'.$attribute_qty_slug;
foreach($product->get_available_variations() as $values){
$attribute_qty_slug_value = $values['attributes'][$attribute_qty_slug_key];
$attribute_qty_name_value = get_term_by('slug', $attribute_qty_slug_value, $attribute_qty_slug);
$variations_id_arr[$values['variation_id']] = __(' per ', 'woocommerce') . strtolower($attribute_qty_name_value->name);
}
## THE JQUERY SCRIPT ##
?>
<script>
(function($){
var $attributes;
<?php
// Passing the product variations attributes array to javascript
$js_array = json_encode($variations_id_arr);
echo 'var $variationsIdsAttr = '. $js_array ;
?>
$('td.value select').blur(function() {
var $variationId = $('input[name="variation_id"]').val();
if (typeof $variationId !== 'undefined'){
for(key in $variationsIdsAttr){
if(key == $variationId){
$attributes = $variationsIdsAttr[key];
break;
}
}
}
if (typeof $attributes !== 'undefined'){
$('.woocommerce-variation-price .woocommerce-Price-amount.amount').append($attributes);
}
});
})(jQuery);
</script>
<?php
}
然后,我们需要改变现有代码,以避免显示第二自定义标签在此具体变量的产品(在第一钩住的函数):
add_filter('woocommerce_variation_price_html','prices_custom_labels', 10, 2);
add_filter('woocommerce_price_html','prices_custom_labels', 10, 2);
function prices_custom_labels($price, $product){
// Custom label name
$per_dozen = ' '. __('per dozen', 'woocommerce');
// Set HERE your "quantity" attribute slug
$attribute_qty_slug = 'pa_quantity';
$attribute_qty_slug_key = 'attribute_'.$attribute_qty_slug;
$append_label = '';
// 1) Variable products
if ($product->product_type != 'simple' && $product->variation_id) {
// Getting the attribute "quantity" value
$attribute_qty_is_set = $product->variation_data[$attribute_qty_slug_key];
echo '<pre>'; print_r($product->variation_data[$attribute_qty_slug_key]); echo '</pre>';
// if "quantity" not set we display " per dozen"
if(! $attribute_qty_is_set)
$append_label = $per_dozen;
// Outputed price + custom label
$price = '<ins class="highlight">'.woocommerce_price($product->regular_price).$append_label.'</ins>';
}
// 2) Simple products
else
{
// Here the output price + custom default label
$price = '<ins class="highlight">'.woocommerce_price($product->regular_price).$per_dozen.'</ins>';
}
return $price;
}
add_filter('woocommerce_variable_price_html', 'prices_custom_labels_min_max', 20, 2);
function prices_custom_labels_min_max($price, $product) {
// Custom label name
$per_dozen = ' '. __('per dozen', 'woocommerce');
$per_case = ' '. __('per case', 'woocommerce');
// Set HERE your quantity attribute slug
$attribute_qty_slug = 'pa_quantity';
// Getting the min and max variations prices
$variation_min_reg_price = $product->get_variation_regular_price('min', true);
$variation_max_reg_price = $product->get_variation_regular_price('max', true);
$variation_reg_price = $product->get_variation_regular_price();
if($variation_min_reg_price == $variation_max_reg_price)
{
$price = '<ins class="highlight">'.woocommerce_price($variation_reg_price) . $per_dozen . '</ins>';
}
else
{
if(!in_array($attribute_qty_slug, array_keys($product->get_attributes())))
{
$price = '<ins class="highlight">' . woocommerce_price($variation_min_reg_price) . $per_dozen . ' - ' . woocommerce_price($variation_max_reg_price) . $per_dozen . '</ins>';
}
else
{
$price = '<ins class="highlight">' . woocommerce_price($variation_min_reg_price) . $per_dozen . ' - ' . woocommerce_price($variation_max_reg_price) . $per_case . '</ins>';
}
}
// print_r($product->get_attributes());
return $price;
}
代码放在functi活动的儿童主题(或主题)的on.php文件或任何插件文件。
相关答案:
它应该是'case'它应该'Case',strstr
是一个区分大小写的函数,因为它是布尔型的,下面的语句将始终返回false。因为您的选择器中的值为Case (20 dozens)
而非case (20 dozens)
请参阅下面的参考资料以了解更多信息。
因此改变下面一行:
$has_case = strstr($last_variation_attribute_slug_value, 'case');
到:
$has_case = strstr($last_variation_attribute_slug_value, 'Case');
无法打开链接! – Smit
刚刚打开没有问题..你得到什么错误?再试一次。 – Dora