【问题标题】:How to get particular product variation quantity dynamically from the cart page in the woocommerce by variation_ID?如何通过variation_ID从woocommerce中的购物车页面动态获取特定的产品变体数量?
【发布时间】:2021-08-26 23:43:40
【问题描述】:

我正在尝试显示添加到购物车的产品变体数量并在循环中动态显示。

我试过这个 PHP 代码:

<?php
$targeted_id = $product->get_id();

// Loop through cart items
foreach ( WC()->cart->get_cart() as $cart_item ) { 
 if( in_array( $targeted_id, array( $cart_item['product_id'],$cart_item['variation_id']) )){
  $quantity =  $cart_item['quantity'];
break; // stop the loop if product is found
 }
}
// Displaying the quantity if targeted product is in cart
if( isset( $quantity ) && $quantity > 0 ) {
  echo  '<a href="http://adel-hat.ru/cart/"><div class="qty_on_product"><i class="tb-icon tb-icon-shopping-cart on_product"></i>';
 printf( '<span class="product-cart-items">' . __("%d") . '</span>', $quantity );
  echo '</span></div></a>';
 }
?>

使用此代码,我将显示添加到购物车的所有变体(因为这里我使用父 ID)并且它仅适用于页面加载。

当变体选择器在没有页面加载的情况下发生变化时,我想动态进入$tageted_id 变量variation_id。我该怎么做?

从我的问题的另一方面,我试图通过 jQuery 获取variation_id

<script>
    jQuery(document).ready(function() {
jQuery( '.variations_form' ).each( function() {
jQuery(this).on( 'found_variation', function( event, variation ) {
    console.log(variation);//all details here
    var variation_id= variation.variation_id;//selectedvariation
    console.log(variation_id);
});
});
});
</script>

当我使用这个时,我会在控制台中获得选定的变体 ID。可能有什么方法可以在不重新加载页面的情况下将variation_id 变量动态发送到 PHP $tageted_id 变量?

【问题讨论】:

    标签: php wordpress woocommerce


    【解决方案1】:

    现在我有了更新功能,因此您可以获取产品选择的变体 ID 并相应地使用:

    function find_matching_product_variation_id($product_id, $attributes)
    {
        return (new \WC_Product_Data_Store_CPT())->find_matching_product_variation(
            new \WC_Product($product_id),
            $attributes
        );
    }
    
    add_action( 'woocommerce_before_cart', 'woo_find_product_in_cart' );
    
    function woo_find_product_in_cart(){
    
        $targeted_product_variation_ids = array();
        $targeted_id = 4215; // product_id 
    
        //Loop through each item from the cart
        foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
            //get the current product ID
            $product_id  =  $cart_item['product_id'];
            $quantity    =  $cart_item['quantity'];
    
            //first - check if product has variations
            if(($targeted_id == $product_id) && (isset($cart_item['variation']) && count($cart_item['variation']) > 0 ))
            {
                // Cart Product Selected Variation id
                $variation_id = find_matching_product_variation_id($product_id,$cart_item['variation']);
    
                //get the WooCommerce Product object by product ID
                $current_product = new  WC_Product_Variable($product_id);
                //get the variations of this product
                $variations = $current_product->get_available_variations();
                //Loop through each variation to get its title
                foreach($variations as $data)
                {
                    $targeted_product_variation_ids[] = $data['variation_id'];
                }
            }else{
                $targeted_product_variation_ids[] = $product_id;
            }
    
            if( in_array( $variation_id, $targeted_product_variation_ids) && isset( $quantity ) && $quantity > 0){
                echo  '<a href="http://adel-hat.ru/cart/"><div class="qty_on_product"><i class="tb-icon tb-icon-shopping-cart on_product"></i>';
                printf( '<span class="product-cart-items">Variation Product Qty: ' . __("%d") . '</span>', $quantity );
                echo '</span></div></a>';
            }
        }
    }
    

    【讨论】:

    • 试过这个,没有得到任何结果。还尝试了不同的钩子以使其显示,例如在我的产品循环模板中使用的 woocommerce_before_shop_loop_item。我正在使用变体样本,我的循环卡看起来像这样:ibb.co/kJFw5S1 有一个例子,我试图在特定产品的购物车中显示数量:ibb.co/SQ7zZs7 我开始猜测我必须使用 javascript 或 Ajax。 ..
    • @Nostromu :我已经应用了一些更改,并且我还在我的购物车页面上测试了它的工作,我们得到了正确的数量。所以试试这个
    猜你喜欢
    • 2017-11-21
    • 2014-05-13
    • 2021-01-26
    • 2021-08-09
    • 2021-11-27
    • 2022-08-17
    • 2017-12-10
    • 2021-05-16
    • 2021-08-29
    相关资源
    最近更新 更多