首页 > 解决方案 > 在购物车 woocommerce 上同步两个产品数量

问题描述

我正在尝试在 woocommerce 的购物车中同步两个产品 ID,以便一个更改会带来另一个数量的更改,我正在尝试此代码,但它会使用我不想要的主产品 ID 更新购物车中的所有产品。

add_action( 'template_redirect', 'bbloomer_sync_cart_quantities' );
    
function bbloomer_sync_cart_quantities() {
    if ( WC()->cart->is_empty() ) return;
   $master_product_id = 20;
   $in_cart = false;
   foreach( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
      if ( $master_product_id === $cart_item['product_id'] ) {
         $qty = $cart_item['quantity'];
         $in_cart = true;
         break;
      }
   }
   if ( ! $in_cart ) return;
   foreach( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
      if ( $master_product_id !== $cart_item['product_id'] ) {
         WC()->cart->set_quantity( $cart_item_key, $qty );
      }
   }     
}

标签: phpjquerywordpresswoocommerce

解决方案


我对此很感兴趣,因为这个问题很难解决。为了让您完成您的要求,您需要知道哪些数量发生了变化。这是一个大问题,因为我认为 PHP 端没有任何钩子可以告诉您这一点。

从我所看到的情况来看,这给我们留下了一个选择,那就是将一些 JavaScript 注入到直接监听更改的购物车页面的底部。这是我写的一个尝试,看看我是否可以让它工作:

function bbloomer_cart_quantity_js() {
    ?>
    <script>    
        jQuery(function($) {
            // hook an event fired when an quantity input box changes on the cart.
            $('.woocommerce').on('change', 'input.qty', function(e) {
                var product_ids = [84647, 84648];   // the synced ids.
                var m_input = $(e.target);          // get the input box that changed.
                var m_qty = m_input.val();          // record the new quantity.
                var m_row = m_input.parents('tr');  // get the cart item row.
                var m_product_id = m_row.find('a.remove[data-product_id]').data('product_id');

                // dont do anything if not the right product.
                if (!product_ids.includes(m_product_id)) return;

                // loop over the remove links to discover product_id matches.
                $('.shop_table a.remove[data-product_id]').each(function(index, element) {
                    var c_link = $(element);
                    var c_row = c_link.parents('tr');
                    var c_product_id = c_link.data('product_id');
                    if (product_ids.includes(c_product_id) && c_product_id != m_product_id) {
                        c_row.find('input.qty').val(m_qty);
                    }
                });

                // clear the last timeout we fired if any, stops too many ajax calls.
                if (window.timeout !== undefined) {
                    clearTimeout(timeout);
                }
            
                // use a timeout to delay and prevent too many ajax updates.
                window.timeout = setTimeout(function() {
                    // update the cart via ajax!
                    $("[name='update_cart']").trigger("click");
                }, 200 );
            });
        });
    </script>
    <?php
}
add_action('woocommerce_after_cart', 'bbloomer_cart_quantity_js');

这绝对可以进一步清理并提高效率,但是我尝试尽可能多地对其进行评论,以便您了解我为什么这样做。我还在我托管的网站上对此进行了测试,并且它有效,因此它应该适合您。


推荐阅读