【问题标题】:Programmatically removing a product from basket以编程方式从购物篮中删除产品
【发布时间】:2019-09-12 09:19:34
【问题描述】:

当购物车总数超过一定数量时,我正在使用以下代码将免费产品添加到购物车中。

当我低于购物车总数时,它会触发 if 语句,但不会删除产品。

我认为这可能是因为购物篮表单上的数量设置为 1,而我的代码没有覆盖该数量以将其设置为 0(并将其删除)。

还有其他方法可以删除或覆盖它吗?

/*
* Automatically adding the product to the cart when cart total amount reach to £20.
*/

function aapc_add_product_to_cart() {
    global $woocommerce;

    $cart_total = 20;
  $free_product_id = 85028;  // Product Id of the free product which will get added to cart

    if ( $woocommerce->cart->total >= $cart_total ) {

    echo "Over the limit";
    $quantity = 1;
    WC()->cart->add_to_cart( $free_product_id, $quantity );

    } elseif($woocommerce->cart->total < $cart_total) {

    echo "Under the limit";
    $quantity = 0;    
    WC()->cart->remove_cart_item( $free_product_id, $quantity );

  }
}

add_action( 'template_redirect', 'aapc_add_product_to_cart' );

我尝试过使用这个question,但无法让它发挥作用,它甚至无法将商品添加到我的购物篮中。

如果有帮助,我正在使用 Woocommerce 版本 3.6.5。

【问题讨论】:

  • 在您的情况下,似乎有一些麻烦,因为 linked answer thread 在上一个 Woocommerce 版本上仍然可以完美运行。它可以是您、您的主题或插件进行的另一种定制。 template_redirect 钩子不适用于 Ajax 迷你购物车操作(删除)或 Ajax 购物车页面操作(更改数量、删除),因为它适用于页面加载。钩子woocommerce_before_calculate_totalswoocommerce_calculate_totals 很方便,因为它们启用了 Ajax。
  • @LoicTheAztec 刚刚让它适用于购物篮页面,不知道为什么链接的答案不起作用,但确实如此(请参阅我的答案)。我可能有链接的答案来覆盖 ajax 部分......所以我对篮子的修复和你对 ajax 部分的答案?
  • 当使用woocommerce_before_calculate_totals钩子时你不能使用cart-&gt;total(因为它不显示任何东西)。相反,您需要使用cart-&gt;subtotal...这是我认为的主要问题。

标签: php woocommerce


【解决方案1】:

我不确定 Woocommerce 是如何工作的,但我假设 remove_cart_item 'quantity' 参数获取要从购物篮中移除的商品数量。因此,将 $quantity 的值设置为 1,它应该会从购物篮中移除一件免费产品。如果篮子里碰巧有不止一件免费物品,那么您需要相应地设置您的 $quantity 值。

此外,当购物篮价值高于 $cart_total 时,每次添加产品时,您都会添加免费商品。也许在添加之前添加一个检查以查看它是否已经存在于篮子中?

希望我正确理解了您的问题

【讨论】:

  • 这就是我最初的想法,但它并没有删除它,所以我认为我可以专门设置一个项目的数量(即设置为零)。总是添加超过 20 件的免费物品确实如此,我会对其进行排序。只需要绕过第一部分。
  • @Rob 试试看以下帖子中的任何答案是否对您有用? stackoverflow.com/questions/30569881/…
【解决方案2】:
/*
* Automatically adding the product to the cart when cart total amount reach to £20.
*/
function aapc_add_product_to_cart() {
    global $woocommerce;

    $cart_total = 20;
  $free_product_id = 85028;  // Product Id of the free product which will get added to cart

    if ( WC()->cart->total >= $cart_total ) {

    $found      = false;
    //check if product already in cart
    if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
        foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
            $_product = $values['data'];
            if ( $_product->get_id() == $free_product_id )
              $found = true;
        }
        // if product not found, add it
        if ( ! $found )
            WC()->cart->add_to_cart( $free_product_id );
    } else {
        // if no products in cart, add it
        WC()->cart->add_to_cart( $free_product_id );
    }

    } elseif(WC()->cart->total < $cart_total) {

    $quantity = 0;
    $prod_unique_id = WC()->cart->generate_cart_id( $free_product_id );
    // Remove it from the cart by un-setting it
    unset( WC()->cart->cart_contents[$prod_unique_id] );
    WC()->cart->remove_cart_item( $free_product_id );

  }
}
add_action( 'woocommerce_after_calculate_totals', 'aapc_add_product_to_cart' );

【讨论】:

  • 您的答案代码部分基于this answer(您可以投票认为它有用)...它不会处理 Ajax 购物车操作(更新数量和删除项目)在对购物车物品进行操作后,您还需要重新加载。此外,您应该删除旧的过时的global $woocommerce; 并将$woocommerce-&gt;cart 替换为WC()-&gt;cart
  • @LoicTheAztec 好的,我已经修改了我的答案,它只适用于使用woocommerce_after_calculate_totals 的包和结帐 (ajax)。在结帐时,如果我超过阈值,它会起作用,但是当我低于阈值时,它不会重新加载 ajax 篮???
猜你喜欢
  • 1970-01-01
  • 2016-06-04
  • 2012-05-18
  • 2020-02-17
  • 2022-08-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多