php,检查购物车是否包含要求 - 添加优惠券

时间:2017-09-13 15:30:28

标签: php wordpress count woocommerce cart

我正在尝试使用php检查购物车在应用免费产品和优惠券之前是否有从阵列中指定的2件商品。我所包含的这个具体例子是检查价格超过一定数量。我试过这个,因为我无法让这两个项目起作用。

function bbloomer_add_gift_if_sku_added_cart( $passed, $product_id, $quantity ) {
global $woocommerce;


$skuswithgift = array('SMWB-M23','001-SLW');


$giftsku = 'comb';

$coupon_code = 'combfree';

$product = wc_get_product( $product_id );

if ($product->get_sku() && in_array($product->get_sku(), $skuswithgift) && $woocommerce->cart->total > 26.00) {
    WC()->cart->add_to_cart( wc_get_product_id_by_sku($giftsku) );
    wc_add_notice( __( 'Hey there! As promised, you recieved a free comb with the purchase of two towels and we added it to your cart for you!', 'woocommerce' ), 'success' );
    $woocommerce->cart->add_discount( $coupon_code );
}

else {
  WC()->cart->remove_cart_item( wc_get_product_id_by_sku($giftsku) );
  $woocommerce->cart->remove_coupon( $coupon_code );
}

return $passed;
}

add_filter( 'woocommerce_add_to_cart_validation', 'bbloomer_add_gift_if_sku_added_cart', 10, 3 );

试过这个并且它无法正常工作

function bbloomer_add_gift_if_sku_added_cart( $passed, $product_id, $quantity ) {
global $woocommerce;

$skuswithgift = array('SMWB-M23','001-SLW');

$giftsku = 'comb';

$coupon_code = 'combfree';

$product = wc_get_product( $product_id );

$total_towels = 0;

// Determine how many towels there are
// Loop through every cart item
foreach ( $woocommerce->cart->get_cart() as $cart_item ) {

  $product = $cart_item['data'];
  //$product = $cart_item;

  // Is it in Gift SKUs
  $is_towel = in_array($product->get_sku(), $skuswithgift);
  if($is_towel){
    // Add this quantity to the total towels
    $total_towels += intval($cart_item['quantity']);
  }

}


// Apply Discount
if ($total_towels <= 2) {
  WC()->cart->add_to_cart( wc_get_product_id_by_sku($giftsku) );
  wc_add_notice( __( 'Hey there! As promised, you recieved a free comb with the purchase of two towels and we added it to your cart for you!', 'woocommerce' ), 'success' );
  $woocommerce->cart->add_discount( $coupon_code );
}

else {
  WC()->cart->remove_cart_item( wc_get_product_id_by_sku($giftsku) );
  $woocommerce->cart->remove_coupon( $coupon_code );
}

return $passed;
}

add_filter( 'woocommerce_add_to_cart_validation', 'bbloomer_add_gift_if_sku_added_cart', 10, 3 );

1 个答案:

答案 0 :(得分:0)

尝试以下代码

add_action('woocommerce_add_to_cart_redirect','customeApplyCode');
function customeApplyCode()
{
    global $woocommerce;    
    $items = $woocommerce->cart->get_cart();
    $productSku = array();
    foreach($items as $item => $values) { 
        // Retrieve WC_Product object from the product-id:
        $product = wc_get_product( $values['product_id']  );
        // Get SKU from the WC_Product object:
        $productSku[] = $product->get_sku();
    }
    $skuswithgift = array('SMWB-M23','001-SLW');
    $result=array_intersect($skuswithgift,$productSku);
    if(count($result)>0)
    {
        //product id of free product
        $product_id = 1312;
        $woocommerce->cart->add_to_cart($product_id);
        add_filter( 'woocommerce_product_needs_shipping', 'hide_shipping_when_free_is_available', 10, 2 );    
    }

}
function hide_shipping_when_free_is_available( $rates, $package ) {
  return false;
}