【问题标题】:Add item to cart automatically when another product is present存在其他产品时自动将项目添加到购物车
【发布时间】:2015-01-15 02:42:42
【问题描述】:

我正在使用动态定价来打折一件物品以免费赠送另一件物品,我想避免让客户将免费产品作为单独的步骤添加,而让系统添加它。

我从 snippet 开始,但当项目存在时我无法让它工作

这是我目前得到的:

<?php

function add_product_to_cart() {
  if ( ! is_admin() ) {
        global $woocommerce;
        $product_id_gift = 2287;
        $found = false;
        $product_id = 30;
        $incart_free = false;

        foreach($woocommerce->cart->get_cart() as $cart_item_key => $values ) {
            $_product = $values['data'];
                if ( $_product->id == $product_id ){
                $incart_free = true;
            }
        return $incart_free;
}
        if( $incart_free == true ) {
            //check if product already in cart
            if ( sizeof( $woocommerce->cart->get_cart() ) > 0 ) {
                foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
                    $_product = $values['data'];
                    if ( $_product->id == $product_id_gift )
                        $found = true;
                }
                // if product not found, add it
                if ( $found != true  )
                    $woocommerce->cart->add_to_cart( $product_id_gift );
            } else {
                // if no products in cart, add it
                $woocommerce->cart->add_to_cart( $product_id_gift );
            }
        }
    }
}

add_action( 'init', 'add_product_to_cart' );

?>

谢谢!

【问题讨论】:

    标签: php wordpress woocommerce product cart


    【解决方案1】:

    如果我正确理解了您的问题,您希望在购物车中有某些商品时免费提供商品。这是我的解决方案:

    1. 在 Wordpress 中为 WooCommerce 创建优惠券。使优惠券金额100%和折扣类型'Product% Discount'。转到使用限制->产品并指定您想要免费的特定产品,这将使优惠券仅适用于该特定产品

    2. 创建一个函数,首先检查购物车中是否有特定商品,如果有,然后将您想要免费的商品添加到购物车并打折。下面的代码就可以解决问题(我已经对其进行了测试并且效果很好,尽管它不是最干净的解决方案):

    add_action( 'init', 'product_discount' );
    
    function product_discount(){
    
    //variable declerations.
    global $woocommerce;
    $product_id = 1; // product to add
    $products= array('2', '3', '4'); //specific product(s) to be present in the cart
    $coupon_code = 'abc'; // coupon code from wp
    
    //get the cart contents.
    $cart_items = $woocommerce->cart->get_cart();   
    //check if the cart is not empty.
    if(sizeof($cart_items) > 0){    
        //loop through the cart items looking for the specific products.
        foreach ($cart_items as $key => $item){     
            //check if the cart items match to any of those in the array and check if the desired product is in the cart.
            if(in_array($item['product_id'], $products) && $item['product_id'] != $product_id){ 
                    //add course.
                    $woocommerce->cart->add_to_cart($product_id);
                    //discount course.
                    $woocommerce->cart->add_discount(sanitize_text_field($coupon_code));
                }else{
                    break; //to prevent the product from being added again for the next loop.
                }
        }           
    }   
    }
    

    希望这会有所帮助!

    【讨论】:

      【解决方案2】:

      你的逻辑在这里是完全错误的,因为if ( $_product-&gt;id == $product_id_gift ) 永远不会是真的,因为两个 product_id 是不同的。

      所以逻辑应该是:

      1. 检查添加到购物车中的所有产品
      2. 检查购物车中的任何产品是否有免费产品
      3. 如果是,则只需添加免费产品。

      所以code 会是这样的:

          add_action( 'init', 'add_product_to_cart' );
      
          function add_product_to_cart() {
          if ( ! is_admin() ) {
          global $woocommerce;
          $product_id = 30; //Your product ID here
          $free_product_id = 2287; //Free product ID here
          $found = false;
          //check if product already in cart
          if ( sizeof( $woocommerce->cart->get_cart() ) > 0 ) {
          foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
          $_product = $values['data'];
          if ( $_product->id == $product_id ){
          $found = true;
          }
          }
          // if product not found, add it
          if ( $found )
          $woocommerce->cart->add_to_cart( $free_product_id );
          }
          }
          }
      

      注意:未经测试。所以也让我知道输出。

      【讨论】:

        猜你喜欢
        • 2023-03-09
        • 1970-01-01
        • 2011-12-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-06-09
        • 1970-01-01
        相关资源
        最近更新 更多