Discount for a second product from the same category in the cart. Woocommerce [on hold]

156
March 19, 2019, at 12:50 PM

Tell me how to make a discount in the woocommerce store: I have several categories. I want for every second product in the category was a 50% discount. This may also apply to variations. Example 1: in cart 5 pizzas for 500, 300, 400, 100 and 200 monetary units. The first product is 500, the product discount is 400 (price after a discount - 200), the second product is 300, the product discount is 200 (price after a discount - 100), the third product for 100 remains without a discount. The discount should go to the second priced product from the "main". I would also like to do it for a variation, for example, if the pizza attribute was 30 or 40 in diameter. Sorry for google translate.

I found and try edit this. But it is not for my case, because It works when a product from a category is added, and it gives a discount on any other goods. Btw: with my category its dont work.

    <?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)
    }
}

And this. Its if a specific product is in cart:

 <?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();
    $product_id = 2216;
    $in_cart = false;
    foreach ( $wc_cart->get_cart() as $cart_item_key => $cart_item ) {
        $cart_product = $cart_item['data'];
        if ( $cart_product->get_id() == $product_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)
    }
}
Rent Charter Buses Company
READ ALSO
Find first occurence (position) of number &gt; 0 from end of string

Find first occurence (position) of number > 0 from end of string

I'm having a bit of an issue or maybe just tunnelvision: I want to get the first number from the end of an string which is not 0It is 100% safe that the given string consists just of numbers

164
Filter Custom Post Type Loop by Year it was posted

Filter Custom Post Type Loop by Year it was posted

I have a custom post type called eguide_landingI have created a custom archive page for that, where I loop through the CPT and display them, split into years

147
Disallow-todo-tests on PHPUnit 7.5.6

Disallow-todo-tests on PHPUnit 7.5.6

I'm trying to use the option phpunit --disallow-todo-tests to ignore some tests that needs to be wrote

117
Laravel routing works only with &#39;/&#39; and not with &#39;/stuff&#39;

Laravel routing works only with '/' and not with '/stuff'

I have just started trying to learn Laravel (58)

131