Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在电子商务中显示所选产品变化的特定数据_Php_Jquery_Wordpress_Woocommerce_Product Variations - Fatal编程技术网

Php 在电子商务中显示所选产品变化的特定数据

Php 在电子商务中显示所选产品变化的特定数据,php,jquery,wordpress,woocommerce,product-variations,Php,Jquery,Wordpress,Woocommerce,Product Variations,在可变产品页面上的“添加到购物车”按钮之后,我正在尝试在我的单个产品页面上显示所选产品变体说明 下面是我实现的代码,但它似乎不起作用: add_action( 'woocommerce_after_add_to_cart_form', 'description_below_add_cart', 11 ); function description_below_add_cart() { global $post; $terms = wp_get_post_terms( $post

在可变产品页面上的“添加到购物车”按钮之后,我正在尝试在我的单个产品页面上显示所选产品变体说明

下面是我实现的代码,但它似乎不起作用:

add_action( 'woocommerce_after_add_to_cart_form', 'description_below_add_cart', 11 );

function description_below_add_cart() {
    global $post;
    $terms = wp_get_post_terms( $post->ID, 'product_cat' );
    foreach ( $terms as $term ) $categories[] = $term->slug;

if ( in_array( 'cookware', $categories ) ) {
        global $product;
        $weight = $product->get_weight();
        $dimensions = wc_format_dimensions($product->get_dimensions(false));
            echo '<div class="short-description">' . $item['product']->get_description() .'</div>';
    }
}
add_action('woocommerce_后加_至_cart_表单','description_后加_至_cart_表单',11);
功能描述\u如下\u添加\u购物车(){
全球$员额;
$terms=wp_get_post_terms($post->ID,'product_cat');
foreach($terms as$term)$categories[]=$term->slug;
if(在_数组中('cookware',$categories)){
全球$产品;
$weight=$product->get_weight();
$dimensions=wc_格式_维度($product->get_维度(false));
回显“”。$item['product']->get_description();
}
}
是网站上的产品页面


添加到购物车按钮后,如何在Woocommerce中显示所选产品变体的特定数据(变体描述)?

更新:关于产品类别部分,您当前的代码可以简化一点。我添加了一些代码来显示变量产品页面中所选产品变体的变体描述

以下是显示所选产品变体说明的方法(需要一些JQuery):

add_action('woocommerce_后加_至_cart_表单','description_后加_至_cart_表单',11);
功能描述\u如下\u添加\u购物车(){
全球$产品;
$terms\u slugs=wp\u get\u post\u terms($product->get\u id(),'product\u cat',array('fields'=>'slugs');//获取术语slugs
//仅适用于“厨具”产品类别
if(在数组中('cookware',$terms\u slug)){
$weight=$product->get_weight();
$weight=!空($weight)?wc_格式_weight($weight):“”;
$dimensions=$product->get_dimensions(false);
$dimensions=!空($dimensions)?wc_格式_维度($dimensions):“”;
//对于可变产品
如果($product->is_类型('variable')){
//显示所选变体说明
回声';
?>
jQuery(函数($){
//在选择变体时,显示变体描述
$('form.variations\u form')。打开('show\u variations',函数(事件,数据){
$('div.selected-variation-description').html(data.variation\u description);
控制台.日志(数据.变量\u说明);
});
//取消选择变体时删除变体描述
$('form.variations\u form')。在('hide\u variations',function()上{
$('div.selected-variation-description').html(“”);
});
});

这确实会显示变量描述,但它会以列表的形式一次显示所有变量。理想情况下,只有从变量下拉列表中选择每个“大小”变量时,变量描述才应可见。谢谢!第二个解决方案有效。我的观点是:)
add_action( 'woocommerce_after_add_to_cart_form', 'description_below_add_cart', 11 );
function description_below_add_cart() {
    global $product;

    $terms_slugs  = wp_get_post_terms( $product->get_id(), 'product_cat', array('fields' => 'slugs') ); // Get the terms  slugs

    // Only for 'cookware' product category
    if ( in_array( 'cookware', $terms_slugs ) ) {
        $weight     = $product->get_weight();
        $weight     = ! empty($weight) ? wc_format_weight($weight) : '';
        
        $dimensions = $product->get_dimensions(false);
        $dimensions = ! empty($dimensions) ? wc_format_dimensions($dimensions) : '';

        // For variable products
        if ( $product->is_type('variable') ) {
            // Display the selected variation description
            echo '<div class="selected-variation-description"></div>';
            ?>
            <script type="text/javascript">
            jQuery( function($){
                // On select variation display variation description
                $('form.variations_form').on('show_variation', function( event, data ){
                    $('div.selected-variation-description').html(data.variation_description);
                    console.log(data.variation_description);
                });
                // On unselect variation remove variation description
                $('form.variations_form').on('hide_variation', function(){
                    $('div.selected-variation-description').html('');
                });
            });
            </script>
            <?php
        }
    }
}