[英]Add multiple products to cart if they are not already in cart
在WooCommerce中我已經實現了@ jtsternberg的 WooCommerce:允許通過添加到購物車的查詢字符串將多個產品添加到購物車,以允許一次添加多個產品,但我收到了許多實際嘗試使用其中一個產品的客戶的投訴包含多個產品的鏈接。
對於初學者,如果客戶點擊結帳然后點擊瀏覽器“返回”按鈕,則所有商品數量都會增加。 我通過在添加到購物車行為完成后將用戶重定向到剝去了任何其他參數的購物車URL來解決這個問題,但這並不理想。
我真正想要的是檢查物品是否首先在購物車中,如果它已經存在則只添加到購物車。 有沒有人做過類似的事情?
工作更新:我最終修改了@jtsternberg中的代碼,以使用完全獨立的參數名稱,以避免與默認的添加到購物車行為發生沖突。 然后,我可以使用@ LoicTheAztec建議的代碼,將行為包裝在一個檢查中,看看是否存在新的參數。 這是完整的部分:
function custom_product_link() {
if (empty( $_REQUEST['multi-product-add'])) {
return;
}
$product_ids = explode( ',', $_REQUEST['multi-product-add'] );
foreach ( $product_ids as $product_id ) {
$product_id = apply_filters( 'woocommerce_add_to_cart_product_id', absint( $product_id ) );
$was_added_to_cart = false;
$adding_to_cart = wc_get_product( $product_id );
if ( ! $adding_to_cart ) {
continue;
}
$add_to_cart_handler = apply_filters( 'woocommerce_add_to_cart_handler', $adding_to_cart->product_type, $adding_to_cart );
if ( 'simple' !== $add_to_cart_handler ) {
continue;
}
// For now, quantity applies to all products.. This could be changed easily enough, but I didn't need this feature.
$quantity = empty( $_REQUEST['quantity'] ) ? 1 : wc_stock_amount( $_REQUEST['quantity'] );
$passed_validation = apply_filters( 'woocommerce_add_to_cart_validation', true, $product_id, $quantity );
if ( $passed_validation && false !== WC()->cart->add_to_cart( $product_id, $quantity ) ) {
wc_add_to_cart_message( array( $product_id => $quantity ), true );
}
}
if ( wc_notice_count( 'error' ) === 0 ) {
// If has custom URL redirect there
if ( $url = apply_filters( 'woocommerce_add_to_cart_redirect', false ) ) {
wp_safe_redirect( $url );
exit;
} elseif ( get_option( 'woocommerce_cart_redirect_after_add' ) === 'yes' ) {
wp_safe_redirect( wc_get_cart_url() );
exit;
}
}
}
function check_product_added_to_cart( $passed, $product_id, $quantity) {
if (!empty( $_REQUEST['multi-product-add'])) {
foreach (WC()->cart->get_cart() as $cart_key => $cart_item ){
// if products are already in cart:
if( $cart_item['product_id'] == $product_id ) {
// Set the verification variable to "not passed" (false)
$passed = false;
// (Optionally) Displays a notice if product(s) are already in cart
// wc_add_notice( '<strong>' . $btn['label'] . '</strong> ' . __( 'This product is already in your cart.', 'woocommerce' ), 'error' );
// Stop the function returning "false", so the products will not be added again
return $passed;
}
}
}
return $passed;
}
add_action( 'wp_loaded', 'custom_product_link', 15 );
add_action( 'woocommerce_add_to_cart_validation', 'check_product_added_to_cart', 10, 3 );
在您使用的代碼中,您可以在其中使用woocommerce_add_to_cart_validation
過濾器鈎子,您可以在自定義鈎子函數中使用它來檢查購物車中是否有罕見的產品,例如:
add_action( 'woocommerce_add_to_cart_validation', 'check_product_added_to_cart', 10, 3 );
function check_product_added_to_cart( $passed, $product_id, $quantity) {
foreach (WC()->cart->get_cart() as $cart_key => $cart_item ){
// if products are already in cart:
if( $cart_item['data']->get_id() == $product_id ) {
// Set the verification variable to "not passed" (false)
$passed = false;
// (Optionally) Displays a notice if product(s) are already in cart
wc_add_notice( '<strong>' . $btn['label'] . '</strong> ' . __( 'This product is already in your cart.', 'woocommerce' ), 'error' );
// Stop the function returning "false", so the products will not be added again
return $passed;
}
}
return $passed;
}
代碼放在活動子主題(或主題)的function.php文件中,或者放在任何插件文件中。
代碼經過測試,這通常適用於您的自定義...
對於產品數量,您可以在某些條件下使用
$quantity
參數和$cart_item['quantity']
...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.