【问题标题】:Sync two product quantities at cart woocommerce在购物车 woocommerce 上同步两个产品数量
【发布时间】:2020-10-10 14:05:32
【问题描述】:

我正在尝试在 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 );
      }
   }     
}

【问题讨论】:

  • 您好,您是否希望所有数量都同步而不管哪一项更改?
  • 当主产品数量发生变化时,它适用于购物车中的所有数量...
  • 我希望它用于特定项目,假设 masterproduct 是 A ,而另一个产品是 B,A 的变化会带来 B 的数量变化,并且不要改变购物车中的其他数量。
  • 我明白了,您是否考虑过您希望如何确定哪些产品应按数量链接?例如,您已经知道 ID 的具体是 2/3/4 产品,还是可以更改?如果是,您打算如何确定哪些同步?
  • 是的,我已经知道我希望同步的两个 ID

标签: php jquery wordpress woocommerce


【解决方案1】:

我对此很感兴趣,因为这个问题很难解决。为了让您完成您的要求,您需要知道哪些数量发生了变化。这是一个大问题,因为我认为 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');

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-18
    • 1970-01-01
    • 2019-10-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多