I have a PHP function I've been working on all day.
I've gotten to this point, and have hit a brick wall.
add_filter( 'wcv_commission_rate', 'my_wcv_commission_rate', 10, 4 );
function my_wcv_commission_rate( $commission, $product_id, $product_price, $order ) {
global $woocommerce;
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
$terms = get_the_terms( $_product->id, 'product_cat' );
foreach ($terms as $term) {
$_categoryid = $term->name;
if($_categoryid == 'T-shirts'){
$per_item_addition = 12;
}
elseif($_categoryid == 'Canvas'){
$per_item_addition = 10;
}
else{
}
}
}
$commission = $product_price - $per_item_addition;
return $commission;
}
The function is is supposed to do this:
- identify each item in the shopping cart
- get their product category names
- store the array of term names from step #2 in
$_categoryid
- pass each array item through the
if elseif
statements individually - subtract each item's
$per_item_addition
amount from$product_price
The function so far does
1 successfully
2 successfully
3 successfully(I used echo $_categoryid
and every products category popped up the correct amount of times. )
4 is where everything stops working. if I have three items in my array which are T-shirt Canvas Canvas the if elseif statement will use 12 for the Canvas as well. Also, when there are duplicate array items they are treated as one item so the function resolves as T-shirts = $product_price - 12
and Canvas = ($product_price * 2) -12
.
So if I have T-shirts Canvas Canvas in my array it will pick up 12 as $per_item_addition
and then use it once on T-shirts and once on Canvas(as opposed to twice)
5 behaves fine as long as there are no identical array items
So concentrating on #4 and #5 I really need to get the array to resolve each item individually So the T-shirts item will pass through the if elseif statement pick up 12 as $per_item_addition
and then subtract that number from $product_price
then start over at Canvas pass it through the if elseif statement and assign 12 to the variable $per_item_addition
and finally subtract it from it's $product_price
, and again for each array item afterwards.
I have no idea what's going wrong. I also tried using Switch statements, it behaves exactly the same, though.
Aucun commentaire:
Enregistrer un commentaire