购物车中的第二个产品-50%(基于类别)

时间:2019-11-24 18:48:55

标签: wordpress woocommerce

我需要在WooCommerce中创建功能-如果第二种产品来自“ odziez”类别,则比第二种产品便宜50%。谁能帮我?此代码不起作用:

<?php
add_action('woocommerce_cart_calculate_fees', 'ts_add_custom_discount', 10, 1 );
function ts_add_custom_discount( $wc_cart ){
    $discount = 0;
    $product_ids = array();
	$in_cart = false;
	foreach ( $wc_cart->get_cart() as $cart_item_key => $cart_item ) {
		$cart_product = $cart_item['data'];
		if ( has_term( 'get2', 'product_cat', $cart_product->get_id() ) ) {
			$in_cart = true;
		}else {
			$product_ids[] = $cart_product->get_id();
		}
	}
	if( $in_cart ) {
		$count_ids = count($product_ids);
		if( $count_ids >= 1 ) { 
		   foreach( $product_ids as $id ) {
			    
				$product = wc_get_product( $id );
				$price = $product->get_price();
				$discount -= ($price * 50) /100; //apply 50% discount on the other product
		   }
	   }
	} 
    if( $discount != 0 ){
        $wc_cart->add_fee( 'Discount', $discount, true  );
        # Note: Last argument in add_fee() method is related to applying the tax or not to the discount (true or false)
    }
}

1 个答案:

答案 0 :(得分:0)

在此,我对产品数量少于或等于一半的产品应用折扣。仅当产品属于odziez类别时才打折。

add_action('woocommerce_cart_calculate_fees', 'ts_add_custom_discount', 10, 1 );
function ts_add_custom_discount( $wc_cart ){
    $discount = 0;
    $product_ids_with_price = array();
    $product_ids = array();
    $in_cart = false;

    $total_odziez = 0;
    $total_discounted = 0;
    // count nr of odziez
    foreach ( $wc_cart->get_cart() as $cart_item_key => $cart_item ) {
        $cart_product = $cart_item['data'];
        if ( has_term( 'odziez', 'product_cat', $cart_product->get_id() ) ) {
            $total_odziez += $cart_item['quantity'];
        }
    }
    foreach ( $wc_cart->get_cart() as $cart_item_key => $cart_item ) {
        $cart_product = $cart_item['data'];
        $prod_id = $cart_product->get_id();
        $_product = wc_get_product( $prod_id ); 
        $price = $_product->get_price();
        if ( has_term( 'odziez', 'product_cat', $prod_id ) ) {
            $product_ids_with_price[$cart_product->get_id()] = $price;
        }
    }

    asort($product_ids_with_price); // sort by price, so we get cheapest on top

    foreach ( $product_ids_with_price as $prod_id => $prod_price ) {
        if ($total_discounted < floor($total_odziez / 2)) {  // Starting from the cheapest, add products for discounting until you have done half of all odziez products
            $total_discounted++;
            $product_ids[] = $prod_price;
            $in_cart = true;
        }    
    }

    if( $in_cart ) {
        $count_ids = count($product_ids);
        if( $count_ids >= 1 ) { 
           foreach( $product_ids as $id => $price ) {

                // $product = wc_get_product( $id );
                // $price = $product->get_price();
                $discount -= ($price * 50) /100; //apply 50% discount on the other product
           }
       }
    } 
    if( $discount != 0 ){
        $wc_cart->add_fee( 'Discount', $discount, true  );
        # Note: Last argument in add_fee() method is related to applying the tax or not to the discount (true or false)
    }
}

更新

对其进行了更改,以使类别odziez的每第二项都打折。

更新

我补充说,最便宜的产品比更昂贵的产品打折。