我已经更改了我的添加到购物车功能,包括两个产品从一个产品页。如果添加了两个产品,它们都将得到一个bundle_id,当我从购物车中删除时,两个产品都会被删除,但是当我撤消时,只有一个产品返回到购物车。我的删除功能:
add_action( 'woocommerce_cart_item_removed', 'cart_remove_func', 10, 2 );
function cart_remove_func($removed_cart_item_key, $cart) {
$line_item = $cart->removed_cart_contents[ $removed_cart_item_key ];
$bundle_id = $line_item[ 'bundle_id' ];
foreach( WC()->cart->get_cart() as $key => $item ){
// Check if the item to be removed 1 is in cart
if( $item['bundle_id'] == $bundle_id ){
WC()->cart->remove_cart_item($key);
}
}
}我尝试将新删除的项添加到removed_cart_contents数组中,但这似乎行不通。
我是否可以将额外的已删除项添加到removed_cart_contents数组中,以便“撤销”将两个产品添加回购物车?
至于购物车更新:当我用bundle_id更新一个项目的数量时,这两个产品都应该设置为相同的数量。我尝试过使用woocommerce_update_cart_action_cart_updated钩子,但我在这里所做的就是显示一个空白页面,并删除购物车中的所有产品(很明显,这不是意图)。
如何使用与更新产品相同的bundle_id更新产品的数量?
发布于 2022-06-23 16:40:21
下面的内容可能适用于撤销,尽管我还没有对它进行充分的测试
add_filter('woocommerce_get_undo_url', 'undo_item_link_for_bundle', 10, 2);
function undo_item_link_for_bundle($link, $key){
$cart_page_url = wc_get_cart_url();
$line_item = WC()->cart->removed_cart_contents[ $key ];
$bundle_id = $line_item[ 'bundle_id' ] ?? false;
if(!$bundle_id){
return $link;
}
foreach(WC()->cart->removed_cart_contents as $k => $v){
if($v['bundle_id'] == $bundle_id){
$keys[] = $k;
}
}
$query_args = array(
'undo_items' => implode(',', $keys),
);
return $cart_page_url ? wp_nonce_url( add_query_arg( $query_args, $cart_page_url ), 'woocommerce-cart' ) : '';
}
add_action( 'wp_loaded', 'undo_multiple_items', 20 );
function undo_multiple_items(){
if ( !isset( $_REQUEST['undo_items'] ) ) {
return;
}
wc_nocache_headers();
$nonce_value = wc_get_var( $_REQUEST['woocommerce-cart-nonce'], wc_get_var( $_REQUEST['_wpnonce'], '' ) );
if ( ! empty( $_GET['undo_items'] ) && isset( $_GET['_wpnonce'] ) && wp_verify_nonce( $nonce_value, 'woocommerce-cart' ) ) {
// Undo Cart Items.
$items = explode(',', $_GET['undo_items']);
foreach($items as $item){
$cart_item_key = sanitize_text_field( wp_unslash( $item ) );
WC()->cart->restore_cart_item( $cart_item_key );
}
$referer = wp_get_referer() ? remove_query_arg( array( 'undo_items', '_wpnonce' ), wp_get_referer() ) : wc_get_cart_url();
wp_safe_redirect( $referer );
exit;
}
}https://stackoverflow.com/questions/72730831
复制相似问题