可变产品选择器:获取实时选定值

在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 = ''.woocommerce_price( $product- >regular_price ).$attribute_quantity_name_value.''; } // 2) Simple products else { // Here the output price + custom default label $price = ''.woocommerce_price( $product- >regular_price ).$per_dozen.''; } return $price; } 

但在可变产品中,我在实时显示的价格中存在附加自定义标签的问题。 我使用的代码仅在实时价格“每打”之后显示。

我需要在自定义“数量”选择器上获取所选值,以便在价格后添加正确的标签:

  • 如果所选的值是“打”,我需要在实时价格“每打”之后显示,
  • 如果选择的值是“Case(20 dozens)”,我需要在实际价格“每箱(20几十)”之后显示。

这个截图是我实际上对所有情况的截图:

在此处输入图像描述

在我的网站特定产品页面上检查此问题

因此,我需要获取属性“数量”选择值,以将正确的标签附加到实时价格。

有帮助吗? 我怎样才能让它工作?

我尝试了很多代码,但我无法使用它。

实现这一目标的唯一方法是使用Javascript / jQuery,但它很复杂,因为WooCommerce已经在其上运行了一些Javascript / Ajax代码。

首先,由于WooCommerce从 html标签中删除"selected"属性, 因此无法在选择器上检测所选客户选择。

一旦客户做出了完整的选择(从这个变量产品中选择了一个变体), Woocommerce隐藏的 html字段中 添加相应的变体ID值并显示相应的价格。

我们的PHP代码将j变量产品的变体ID数组传递给javascript, 为每个变量产生相应的“数量”属性值

然后我们可以使用 html标签上的“on blur” javascript事件来获取隐藏的variation ID值,然后使用正确的“标签”附加价格

以下是function代码,它将根据客户选择(因此选择的产品变体)为实时价格添加自定义标签:

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

然后我们需要更改现有代码,以避免在此特定变量产品上显示第二个自定义标签(在第一个钩子函数中):

 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 '
'; print_r($product->variation_data[$attribute_qty_slug_key]); echo '

'; // if "quantity" not set we display " per dozen" if( ! $attribute_qty_is_set ) $append_label = $per_dozen; // Outputed price + custom label $price = ''.woocommerce_price( $product->regular_price ).$append_label.''; } // 2) Simple products else { // Here the output price + custom default label $price = ''.woocommerce_price( $product->regular_price ).$per_dozen.''; } 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 = ''.woocommerce_price($variation_reg_price) . $per_dozen . ''; } else { if( !in_array( $attribute_qty_slug, array_keys( $product->get_attributes() ) ) ) { $price = '' . woocommerce_price($variation_min_reg_price) . $per_dozen . ' - ' . woocommerce_price($variation_max_reg_price) . $per_dozen . ''; } else { $price = '' . woocommerce_price($variation_min_reg_price) . $per_dozen . ' - ' . woocommerce_price($variation_max_reg_price) . $per_case . ''; } } // print_r($product->get_attributes()); return $price; }

代码放在活动子主题(或主题)的function.php文件中,或者放在任何插件文件中。


相关回答:

  • 将选定的产品变体数据传递到Contact Form 7查询表
  • 根据类型将自定义文本标签添加到产品价格中

它应该是“case”它应该是’Case’, strstr是一个区分大小写的函数,因为它是boolean,下面的语句将始终返回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'); 

参考: https : //www.w3schools.com/php/func_string_strstr.asp