【问题标题】:Check if a product variation is in cart in Woocommerce在 Woocommerce 中检查产品变体是否在购物车中
【发布时间】:2018-10-13 12:37:48
【问题描述】:

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

这是我的代码,适用于产品类型不是变量的情况。

<?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 的情况下工作?

【问题讨论】:

  • 所以只是为了澄清这一点:你想用php(在后端)检查在后端时动态添加到前端(客户端)的新产品吗?你有两个解决方案:1. 在前端做,2. 创建一个 ajax 并在后端做(但不会是初始时间)
  • @Edwin 我打算在将产品添加到购物车后或浏览产品页面时显示文本(如果产品已经在购物车中)。该功能适用​​于简单产品,因为产品 ID 和购物车项目 ID 相同。但是对于可变产品,变体 id 和产品 id 不存在。

标签: php jquery wordpress woocommerce variations


【解决方案1】:

您的意思是 $product_id 可能是变体的 ID?如果是这样,您可以只获取父 ID(如果存在):

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

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

    $parent_id  = wp_get_post_parent_id( $product_id );
    $product_id = $parent_id > 0 ? $parent_id : $product_id;

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

如果您的意思是您的购物车商品是一个变体,并且 $product_id 已经是父产品 ID,那么您的代码应该已经可以正常工作了。

$cart_item 有 2 个 ID:$cart_item['product_id']$cart_item['variation_id']

所以product_id 将始终是父产品的。

【讨论】:

  • 是的。 $product_id 可能是变体 ID。我只是没有在我的代码中添加条件来声明它。但是,我不知道在没有 jquery 的情况下获取变体 ID(如果它不是默认变体)。
  • 如果您解释一下您想在什么时候获取变体 ID,也许会有所帮助?
  • 对不起,我的错误。我想在页面加载时(在默认变体的情况下)和选择变体选项后获取变体 ID。
  • 如果没有 javascript,您将无法做到这一点。当您选择变体时,会在变体表单上触发 found_variations 事件,这会将变体对象传递给其中,然后您需要通过 AJAX 进行检查。
  • 在加载时您可以使用 PHP。
【解决方案2】:

处理单个产品页面之外的产品变体(以及任何地方的简单产品):

// 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。

当所选变体已在购物车中时,以下示例将显示自定义消息:

// 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 文件中。经过测试并且可以工作。

【讨论】:

    猜你喜欢
    • 2017-05-06
    • 1970-01-01
    • 1970-01-01
    • 2019-03-17
    • 2018-12-10
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 2014-05-13
    相关资源
    最近更新 更多