检查Woocommerce中购物车中是否有产品变化 [英] Check if a product variation is in cart in Woocommerce

查看:147
本文介绍了检查Woocommerce中购物车中是否有产品变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试显示购物车中是否已经存在某个产品的变体(在单个产品页面中).产品ID与购物车对象中产品的简单比较不适用于可变产品,因为使用Ajax加载了版本ID.

I am trying display if a variation of a product is already in a cart or not (in single product page). A simple comparing of the product id with products in cart object is not working for variable product as the variation id is being loaded using ajax.

这是我的代码,该代码可以在产品类型不是变量的情况下使用.

Here is my code that works in case the product type is other than variable.

<?php
/*
 * Check if Product Already In Cart
*/
function woo_in_cart( $product_id ) {
    global $woocommerce;

    if ( !isset($product_id) ) {
        return false;
    }

    foreach( $woocommerce->cart->get_cart() as $cart_item ) {
        if ( $cart_item['product_id'] === $product_id ){
            return true;
        } else {
            return false;
        }
    }
}  

有没有什么方法可以使它在没有jQuery的情况下工作?

Is there any way to make it work without jQuery?

推荐答案

要处理单个产品页面(以及到处都是简单产品)之外的产品变化:

To handle product variations outside single product pages (and simple products everywhere):

// Check if Product Already In Cart (Work with product variations too)
function woo_in_cart( $product_id = 0 ) {
    $found = false;
    if ( isset($product_id) || 0 == $product_id )
        return $found;

    foreach( WC()->cart->get_cart() as $cart_item ) {
        if ( $cart_item['data']->get_id() == $product_id )
            $found = true;
    }
    return $found;
}

要处理单个产品页面内的产品变体,需要使用javascript.

To handle product variations inside single product pages, javascript is needed.

以下是当购物车中已包含所选版本时显示自定义消息的示例:

Here is an example that will show a custom message, when the selected variation is already in cart:

// Frontend: custom select field in variable products single pages
add_action( 'wp_footer', 'action_before_add_to_cart_button' );
function action_before_add_to_cart_button() {
    if( ! is_product() ) return;

    global $product;

    if( ! is_object($product) )
        $product = wc_get_product( get_the_id() );

    // Only for variable products when cart is not empty
    if( ! ( $product->is_type('variable') && ! WC()->cart->is_empty() ) ) return; // Exit

    $variation_ids_in_cart = array();

    // Loop through cart items
    foreach( WC()->cart->get_cart() as $cart_item ) {
        // Collecting product variation IDs if they are in cart for this variable product
        if ( $cart_item['variation_id'] > 0 && in_array( $cart_item['variation_id'], $product->get_children() ) )
            $variation_ids_in_cart[] = $cart_item['variation_id'];
    }

    // Only if a variation ID for this variable product is in cart
    if( sizeof($variation_ids_in_cart) == 0 ) return; // Exit

    // Message to be displayed (if the selected variation match with a variation in cart
    $message = __("my custom message goes here", "woocommerce");
    $message = '<p class="custom woocommerce-message" style="display:none;">'.$message.'</p>';

    // jQuery code
    ?>
    <script>
    (function($){
        // Utility function that check if variation match and display message
        function checkVariations(){
            var a = 'p.woocommerce-message.custom', b = false;
            $.each( <?php echo json_encode($variation_ids_in_cart); ?>, function( k, v ){
                if( $('input[name="variation_id"]').val() == v ) b = true;
            });
            if(b) $(a).show(); else $(a).hide();
        }

        // On load (when DOM is rendered)
        $('table.variations').after('<?php echo $message; ?>');
        setTimeout(function(){
            checkVariations();
        }, 800);

        // On live event: product attribute select fields "blur" event
        $('.variations select').blur( function(){
            checkVariations();
        });
    })(jQuery);
    </script>
    <?php
}

代码进入您的活动子主题(或活动主题)的function.php文件中.经过测试,可以正常工作.

Code goes in function.php file of your active child theme (or active theme). Tested and works.

这篇关于检查Woocommerce中购物车中是否有产品变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆