我已经建立了一个WooCommerce商店来出售舞蹈工作室的门票。一切都很好,但是如果有人在同一时间放两张工作坊的门票,我会从购物车中删除门票。
示例:我已为该课程制作了10张门票。
现在,当有人将A1添加到购物车时,他不能使用相同的时程B1。
我使用以下代码,仅对 1个产品
有效add_action( 'woocommerce_add_to_cart', 'check_product_added_to_cart', 10, 6 );
function check_product_added_to_cart($cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data) {
// Set HERE your targeted product ID
$target_product_id = 31;
// Set HERE the product ID to remove
$item_id_to_remove = 37;
// Initialising some variables
$has_item = false;
$is_product_id = false;
foreach( WC()->cart->get_cart() as $key => $item ){
// Check if the item to remove is in cart
if( $item['product_id'] == $item_id_to_remove ){
$has_item = true;
$key_to_remove = $key;
}
// Check if we add to cart the targeted product ID
if( $product_id == $target_product_id ){
$is_product_id = true;
}
}
if( $has_item && $is_product_id ){
WC()->cart->remove_cart_item($key_to_remove);
// Optionaly displaying a notice for the removed item:
wc_add_notice( __( 'The product "blab bla" has been removed from cart.', 'theme_domain' ), 'notice' );
}
}
有人可以解释如何修改此代码?
答案 0 :(得分:0)
以下代码适用于相应的产品,如果存在一个产品ID,则会从购物车中删除相应的产品ID。
更新:12/20-改进的代码
function action_woocommerce_add_to_cart( $cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data ) {
// (USE: PRODUCT_IDs) - the corresponding product id's
// Example: if product with ID 30 is added and product with ID 32 is in cart, product with ID 32 will be removed from cart, this also works the opposite
$associated_product_ids_arrays = array(
array(
'product_id_1' => '30',
'product_id_2' => '32',
),
array(
'product_id_1' => '817',
'product_id_2' => '819',
),
array(
'product_id_1' => '827',
'product_id_2' => '843',
),
);
// check
$found = false;
// Loop trough arrays
foreach ( $associated_product_ids_arrays as $key_1 => $associated_product_ids_array ) {
foreach ( $associated_product_ids_array as $key_2 => $associated_product_ids ) {
// Compare
if ( $associated_product_ids == $product_id ) {
// Unset
unset( $associated_product_ids_array[$key_2] );
// Convert last value in array to integer
$associated_ticket_id = (int) implode('', $associated_product_ids_array );
// Found = true, break 2 loops
$found = true;
break 2;
}
}
}
// True
if ( $found ) {
// Generate a unique ID for the cart item
$product_cart_id = WC()->cart->generate_cart_id( $associated_ticket_id );
// Check if product is in the cart and return cart item key
$item_key = WC()->cart->find_product_in_cart( $product_cart_id );
// if item_key true
if ( $item_key ) {
// remove product from cart
WC()->cart->remove_cart_item( $item_key );
// Optionaly displaying a notice
wc_add_notice( sprintf( __( 'The product %d has been removed from cart because product %d has been added.', 'woocommerce' ), $associated_ticket_id, $product_id ), 'notice' );
}
}
}
add_action( 'woocommerce_add_to_cart', 'action_woocommerce_add_to_cart', 10, 6 );
从03/20开始回答
function product_to_remove( $cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data ) {
// (USE: PRODUCT_IDs) - the corresponding ticket id's
$associated_ticket_ids_1 = array( 30, 32 );
$associated_ticket_ids_2 = array( 817, 819 );
$associated_ticket_ids_3 = array( 827, 843 );
// etc...
// total of associated ticket ids arrays, total = number of different $associated_ticket_ids arrays, in this example: 3
$total = 3;
// check
$found = false;
// loop through the arrays
for ($i = 1; $i <= $total; $i++) {
$array_name = ${'associated_ticket_ids_' . $i};
foreach ($array_name as $ticket_id ) {
if ( $ticket_id == $product_id ) {
/* Get the associated ticket id */
// Search value and unset
unset( $array_name[array_search( $ticket_id, $array_name )] );
// Convert last value in array to integer
$associated_ticket_id = (int) implode('', $array_name);
// if found, break loops
$found = true;
break 2;
/* uncomment line below for debug purposes */
//echo 'Product id: ' . $product_id . ' | Ticket id: ' . $ticket_id . ' | Associated ticket id: ' . $associated_ticket_id;
}
}
}
// if found true
if ( $found ) {
// Generate a unique ID for the cart item
$product_cart_id = WC()->cart->generate_cart_id( $associated_ticket_id );
// Check if product is in the cart and return cart item key
$item_key = WC()->cart->find_product_in_cart( $product_cart_id );
// if item_key true
if ( $item_key ) {
// remove product from cart
WC()->cart->remove_cart_item( $item_key );
// Optionaly displaying a notice
wc_add_notice( __( 'The product ' . $associated_ticket_id . ' has been removed from cart because product ' . $product_id . ' has been added.', 'woocommerce' ), 'notice' );
}
}
}
add_action( 'woocommerce_add_to_cart', 'product_to_remove', 10, 6 );