Reputation: 101
I have modified the functions.php file to include the possibility of customizing certain products with custom text.
This feature comes with an extra cost of 1 which will be shown on product checkout unless the feature hasn't been fulfilled:
if (!empty( $cart_item['custom_text'] ) ) {
$surcharge = 1;
WC()->cart->add_fee( 'Customization', $surcharge, true, '' );
}
Naturally, if more customized products are added to the cart, this 1 should be multiplicated by the number of products in the cart. I've tried adding the variable $quantity for this purpose:
if (!empty( $cart_item['custom_text'] ) ) {
$quantity = WC()->cart->get_cart_contents_count();
$surcharge = 1 * $quantity;
WC()->cart->add_fee( 'Customization', $surcharge, true, '' );
}
The problem here is if the client attempts to buy a customized item but not the other, since this method takes into account the total quantity in the cart regardless of it's customized or not. See figure 1:
So the question is: is it possible to get only the customized items (circled in green in figure 1) to calculate the surcharge?
Upvotes: 3
Views: 140
Reputation: 253804
To get the cart item quantity for your customized product only use the following:
if (!empty( $cart_item['custom_text'] ) ) {
$surcharge = 1;
$quantity = $cart_item['quantity'];
WC()->cart->add_fee( 'Customization', ( $surcharge * $quantity ), true, '' );
}
It should works as you are expecting
Upvotes: 2