Hallo,
ich habe ein Script, dass mir automatisch ein Produkt (ID = 11) in den Warenkorb legt, sobald der Gesamtpreis des Warenkorbs 30€ überschreitet.
Nun möchte ich, dass das Produkt 11 nur in den Warenkorb gelegt, wird sobald mind 2 der PRodukte mit der ID 22 enthalten sind.
Und wenn vier Produkte der ID 22 im Warenkorb sind, sollen 2 Produkte der ID 11 automatisch hineingelegt werden.
Wie passe ich das Script an?
Code
add_action( [COLOR=#DF5000]'init'[/COLOR], [COLOR=#DF5000]'add_product_to_cart'[/COLOR] );
[COLOR=#A71D5D]function[/COLOR] [COLOR=#795DA3]add_product_to_cart[/COLOR]() {
[COLOR=#A71D5D]if[/COLOR] ( [COLOR=#A71D5D]![/COLOR] is_admin() ) {
[COLOR=#A71D5D]global[/COLOR] $woocommerce;
$product_id [COLOR=#A71D5D]=[/COLOR] [COLOR=#0086B3]11[/COLOR];
$found [COLOR=#A71D5D]=[/COLOR] [COLOR=#0086B3]false[/COLOR];
$cart_total [COLOR=#A71D5D]=[/COLOR] [COLOR=#0086B3]30[/COLOR];
[COLOR=#A71D5D]if[/COLOR]( $woocommerce[COLOR=#A71D5D]->[/COLOR]cart[COLOR=#A71D5D]->[/COLOR]total [COLOR=#A71D5D]>=[/COLOR] $cart_total ) {
[COLOR=#969896]//check if product already in cart[/COLOR]
[COLOR=#A71D5D]if[/COLOR] ( [COLOR=#0086B3]sizeof[/COLOR]( $woocommerce[COLOR=#A71D5D]->[/COLOR]cart[COLOR=#A71D5D]->[/COLOR]get_cart() ) [COLOR=#A71D5D]>[/COLOR] [COLOR=#0086B3]0[/COLOR] ) {
[COLOR=#A71D5D]foreach[/COLOR] ( $woocommerce[COLOR=#A71D5D]->[/COLOR]cart[COLOR=#A71D5D]->[/COLOR]get_cart() [COLOR=#A71D5D]as[/COLOR] $cart_item_key [COLOR=#A71D5D]=>[/COLOR] $values ) {
$_product [COLOR=#A71D5D]=[/COLOR] $values[[COLOR=#DF5000]'data'[/COLOR]];
[COLOR=#A71D5D]if[/COLOR] ( $_product[COLOR=#A71D5D]->[/COLOR]id [COLOR=#A71D5D]==[/COLOR] $product_id )
$found [COLOR=#A71D5D]=[/COLOR] [COLOR=#0086B3]true[/COLOR];
}
[COLOR=#969896]// if product not found, add it[/COLOR]
[COLOR=#A71D5D]if[/COLOR] ( [COLOR=#A71D5D]![/COLOR] $found )
$woocommerce[COLOR=#A71D5D]->[/COLOR]cart[COLOR=#A71D5D]->[/COLOR]add_to_cart( $product_id );
} [COLOR=#A71D5D]else[/COLOR] {
[COLOR=#969896]// if no products in cart, add it[/COLOR]
$woocommerce[COLOR=#A71D5D]->[/COLOR]cart[COLOR=#A71D5D]->[/COLOR]add_to_cart( $product_id );
}
}
}
}
Alles anzeigen