Начиная с Woocommerce 3, хук woocommerce_get_price
устарел и заменен.Кроме того, код действительно устарел, полон ошибок и ошибок.
Вы не можете реально изменить цену товара, основываясь на корзине, так как она всегда будет давать ошибки, и это не правильный способ справиться с тем, что вы хотели бы.
В любом случае, вот ваш повторный код, , но вы не должны его использовать (см. Другой способ ниже) :
add_filter( 'woocommerce_product_get_price', 'custom_specific_product_prices', 10, 2 );
function custom_specific_product_prices( $price, $product ) {
// Exit when cart is empty
if( WC()->cart->is_empty() )
return $price; // Exit
## ----- Your settings below ----- ##
$countries = array('GR'); // Country codes
$product_ids = array('1151', '1152'); // Product Ids
$container = 3000; // Container cost
## ------------------------------- ##
if( ! in_array( $product->get_id(), $product_ids ) )
return $price; // Exit
$cart_items_count = WC()->cart->get_cart_contents_count();
$shipping_country = WC()->customer->get_shipping_country();
// If the customers shipping country is in the array and the post id matches
if ( in_array( $shipping_country, $countries ) ) {
// Return the price plus the $amount
$price += $container / $cart_items_count;
}
return $price;
}
Кодидет в файл function.php вашей активной дочерней темы (или активной темы).Протестировано и работает.
Что вы можете сделать, это добавить плату "Контейнер":
add_action( 'woocommerce_cart_calculate_fees', 'add_container_fee', 10, 1 );
function add_container_fee( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return; // Exit
## ----- Your settings below ----- ##
$countries = array('GR'); // Country codes
$product_ids = array('1151', '1152'); // Product Ids
$container = 3000; // Container cost
## ------------------------------- ##
$shipping_country = WC()->customer->get_shipping_country();
$items_found = false;
if ( ! in_array( $shipping_country, $countries ) )
return; // Exit
foreach( $cart->get_cart() as $cart_item ) {
if ( array_intersect( array( $cart_item['variation_id'], $cart_item['product_id'] ), $product_ids ) )
$items_found = true; // Found
}
if ( $items_found )
$cart->add_fee( __('Container fee'), $container );
}
Код входит в файл function.php вашей активной дочерней темы (или активнойтема).Проверено и работает.