Скидка за количество на 2-й товар только в Woocommerce

Деструктурирование массива на самом деле является Истребитель разрушения , который работает с чем-либо, реализующим метод Symbol.iterator.

Например

function foo([a, b, c]) {
  console.log([a, b, c])
}

foo({
  * [Symbol.iterator]() {
    yield 1;
    yield 2;
    yield 3;
  }
})

Number не реализует протокол итератора

console.log(1[Symbol.iterator])

Вот почему вы получаете ошибку.

Но если вы его реализуете (НЕ РЕКОМЕНДУЕТСЯ)

Number.prototype[Symbol.iterator] = function*() {
  yield * this.toString(2); // just an example
}

function foo([a, b,c]) {
  console.log(a, b, c);
}

foo(6)

1
задан LoicTheAztec 19 January 2019 в 13:01
поделиться

3 ответа

add_action( 'woocommerce_before_calculate_totals', 'set_the_discount' );

function set_the_discount( $cart ) {

foreach ( $cart->cart_contents as $key => $value ) {    

    //please check whether your item is the second item of your cart or not?

    //if this is your second item in your cart

    //Set the 50% discount

    $product_id = $value['product_id'];
    $product = wc_get_product($product_id);
    $get_price = $product->get_price();
    $new_price = $get_price / 2;
    $value['data']->set_price($new_price);
  }

}
This hook can be used to set the price directly to the cart. Hope this may help
0
ответ дан Risha Tiwari 19 January 2019 в 13:01
поделиться

Вы можете использовать Fee API для получения скидки (20%) на 2-й товар только так:

add_action( 'woocommerce_cart_calculate_fees', 'second_item_discount', 10, 1 );
function second_item_discount( $cart ) {

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    $percentage = 20; // 20%
    $discount   = 0;

    // Loop through cart items
    foreach ( $cart->get_cart() as $cart_item ) {
        // When quantity is more than 1
        if( $cart_item['quantity'] > 1 ){
            // 20% of the product price as a discount
            $discount += wc_get_price_excluding_tax( $cart_item['data'] ) * $percentage / 100;
        }
    }
    if( $discount > 0 )
        $cart->add_fee( __( '2nd item discount', 'woocommerce' ) , -$discount );
}

Код находится в файле function.php вашей активной дочерней темы (или активной темы). ). Проверено и работает.

0
ответ дан LoicTheAztec 19 January 2019 в 13:01
поделиться
foreach ( $cart->cart_contents as $key => $values ) {
                    $product_id = $values['product_id'];

                    foreach ($cart->get_cart_item_quantities() as $key => $value){
                        //print_r($key[1]);
                        $key = array_keys($cart->get_cart_item_quantities())[1];
                        if($key == $product_id){

                            $product = wc_get_product($product_id);
                            $get_price = $product->get_price();
                            $new_price = $get_price / 2;
                            $values['data']->set_price($new_price);

                            break;
                        }
                    }
                }
//Please try this, it will work for sure in same above hook :)
0
ответ дан Risha Tiwari 19 January 2019 в 13:01
поделиться
Другие вопросы по тегам:

Похожие вопросы: