Я пытаюсь рассчитать CRV в корзине на основе атрибута размера. < /p>
Я нашел код, как показано ниже. Он работает для идентификатора продукта, но мне нужны 2000+ продуктов. < /P>
add_action( 'woocommerce_cart_calculate_fees', 'action_woocommerce_cart_calculate_fees', 10, 1 );
function action_woocommerce_cart_calculate_fees( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// Settings: amount / product Ids
$settings = array(
'0.05' => array( 11891, 11893 ),
'0.1' => array( 11892 ),
'0.2' => array( 825 ),
);
$total_amount = 0; // Initializing
// Loop through cart items
foreach ( $cart->get_cart() as $item ) {
// Loop trough settings array
foreach ( $settings as $amount => $product_ids ) {
// Search for the product ID
if ( in_array( $item['product_id'], $product_ids) ) {
$total_amount += floatval($amount) * intval($item['quantity']); // Add to total amount
}
}
}
if ( $total_amount > 0 ) {
$cart->add_fee( __( 'CRV', 'woocommerce' ), $total_amount, false );
}
}
< /code>
Могу ли я сделать массив с атрибутами? Такие как 0,05 для 50 мл, 0,1 для 100 мл.
Подробнее здесь: https://stackoverflow.com/questions/795 ... s-function