Изменять только промежуточную сумму товара в корзине на основе веса товара в WooCommerce - PullRequest
0 голосов
/ 16 февраля 2020

Я пытался обновить итоги корзины после выполнения расчета ..

add_action( 'woocommerce_before_calculate_totals', 'set_cart_item_calculated_price', 10, 1 );
function set_cart_item_calculated_price( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    // Required since Woocommerce version 3.2 for cart items properties changes
    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        return;

    // Loop through cart items
    foreach ( $cart->get_cart() as $cart_item ) {
        // Set the new calculated price based on lenght
        if( isset($cart_item['data']) ) {
            $cart_item['data']->set_price( $cart_item['data']->get_price() * $cart_item['data']->get_weight() );
        }
    }
}

Работает нормально , но меняет обычную цену продукта , которую я не хочу.


Этот другой крючок не меняет цену товара, но не обновляет итоги корзины:

add_filter('woocommerce_cart_product_subtotal', 'filter_woocommerce_cart_product_subtotal', 10, 4);
function filter_woocommerce_cart_product_subtotal($product_subtotal, $product, $quantity, $cart) {
    $new_subtotal = $product_subtotal;
    $weight = $product->get_weight();
    $price = $product->get_price();
    if($weight && $price){
        $new_subtotal = ($price * (float)$weight) * $quantity;
    }
    return wc_price($new_subtotal);
}

Как изменить только каждый промежуточный итог корзины в зависимости от веса товара? Любая помощь приветствуется.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...