【问题标题】:Single Woocommerce product custom variations add to cart buttons单个 Woocommerce 产品自定义变体添加到购物车按钮
【发布时间】:2019-07-16 10:34:09
【问题描述】:

我已经自定义了 WoCommerce 单品页面,并仅使用这一行添加了一个购物车按钮

echo woocommerce_simple_add_to_cart();

但现在我需要两个添加两个购物车按钮来实现两种类型的变化。我的所有属性和孩子都是一样的。 type_of_audio 和 child 是 mac & windows。

我想创建两个自定义添加到购物车链接。但我无法动态配置产品 ID。底部是代码。 (可变产品 ID 未正确获取,因此未将产品添加到购物车)

<?php if( $product->is_type( 'simple' ) ){                             
   echo woocommerce_simple_add_to_cart();                                
}                          
elseif( $product->is_type( 'variable' ) ){
   echo ('<a href="/?add-to-cart=$variation_id&attribute_pa_type_of_audio=mac">MAC BUTTON</a>');
   echo ('<a href="/?add-to-cart=$variation_id&attribute_pa_type_of_audio=windows">WINDOWS BUTTON</a>');
                                }
?>

【问题讨论】:

  • 如果产品是变量,则根本不会调用该模板。您必须在模板层次结构中向上查找。

标签: php wordpress woocommerce variations taxonomy-terms


【解决方案1】:

尝试以下操作,为每个变体显示一个添加到购物车的按钮,并带有正确的按钮标签:

<?php 
if ( $product->is_type( 'simple' ) ){
   echo woocommerce_simple_add_to_cart();
}
elseif ( $product->is_type( 'variable' ) ){
    // Loop through available variation data
    foreach ( $product->get_available_variations() as $variation_data ) {
        $url = '?add-to-cart='.$variation_data['variation_id']; // The dynamic variation ID (URL)

        // Loop through variation product attributes data
        foreach ( $variation_data['attributes'] as $attr_key => $term_slug ) {
            // The text button
            if ( $attr_key === 'attribute_pa_type_of_audio' && $term_slug === 'mac' ) 
            { // MAC
                $button_text = __("MAC BUTTON", "woocommerce");
            } elseif  ( $attr_key === 'attribute_pa_type_of_audio' && $term_slug === 'windows' ) 
            { // WINDOWS
                $button_text = __("WINDOWS BUTTON", "woocommerce");
            } else 
            { // OTHER (IF NEEDED)
                $button_text = __("Add to cart", "woocommerce"); 
            }

            $url .= '&'.$attr_key.'='.$term_slug; // Adding the product attribute name with the term value (to Url)
        }
        // Displaying the custom variations add to cart buttons
        if( isset($button_text))
            echo '<a href="'.$url.'" class="button alt">'.$button_text.'</a> ';
    }
}
?>

经过测试并且有效。

【讨论】:

    【解决方案2】:
     if ($product->is_type('simple')) {
        echo woocommerce_simple_add_to_cart();
    } elseif ($product->is_type('variable')) {
    
    
        $children_ids = $product->get_children();
    
        foreach ($children_ids as $value) {
            $single_variation = new WC_Product_Variation($value);
            echo '<a href="/?add-to-cart=$variation_id=' . $value . '&attribute_pa_type_of_audio=mac">MAC BUTTON</a>';
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2020-09-01
      • 2015-12-04
      • 1970-01-01
      • 1970-01-01
      • 2019-12-13
      • 2018-03-05
      • 2022-01-03
      • 2015-05-27
      • 2018-06-08
      相关资源
      最近更新 更多