Я пытаюсь установить стоимость доставки на 0 или изменить класс доставки на бесплатную доставку для конкретных продуктов. /> Это работает, но здесь у моего поставщика платежей есть проблема, потому что отрицательные значения не допускаются: < /p>
$cart->add_fee( 'Versandkostenfrei', -$cart->get_shipping_total(), true, '' );`
< /code>
Поле «Unitprice» не может быть отрицательным, если только «тип» не является одним из
Следующее: 'Discount', 'store_credit' и 'dift_card'. Ноль.
Это также неплохое представление клиента. < /p>
Поэтому я попробовал: < /p>
function set_shipping_costs_to_zero( $rates ) {
foreach ( $rates as $rate_id => $rate ) {
$rates[ $rate_id ]->cost = 0;
if ( isset( $rates[ $rate_id ]->taxes ) && is_array( $rates[ $rate_id ]->taxes ) ) {
foreach ( $rates[ $rate_id ]->taxes as $key => $value ) {
$rates[ $rate_id ]->taxes[ $key ] = 0;
}
}
}
return $rates;
}
< /code>
, который не устанавливает цену на 0.unset( $rates[$rate_id] );
< /code>
использовал функцию в следующих крючках без успеха: < /p>
[*] woocommerce_before_calculate_totals, < /li>
woocommerce_package_rates, < /li>
/> woocommerce_cart_calculate_fees.
< /ul>
Есть идеи? < /p>
Полный код: < /p>
add_action( 'woocommerce_cart_calculate_fees', 'apply_free_shipping_for_conditions', 10, 1 );
function apply_free_shipping_for_conditions( $cart ) {
// Run only if checkout or cart
if ( ! is_cart() && ! is_checkout() ) {
return;
}
// cycle all products in cart
foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
$product_id = $cart_item['product_id'];
// check custom fields
$free_shipping = get_post_meta( $product_id, '_free_shipping', true );
$merchant = get_post_meta( $product_id, '_merchant', true );
$sku = get_post_meta( $product_id, '_sku', true );
// if condition true remove shipping costs
if ( 'yes' === $free_shipping || 'vidaxl' === strtolower( $merchant ) || 'vevor' === strtolower( $merchant ) || ( strpos( $sku, 'VD-' ) === 0 )) {
$cart->add_fee( 'Versandkostenfrei', -$cart->get_shipping_total(), true, '' );
break;
}
}
}
Подробнее здесь: https://stackoverflow.com/questions/795 ... e-shipping
Как удалить расходы на доставку или изменить бесплатную доставку? [закрыто] ⇐ Php
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение