久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

一次將多個項目添加到 WooCommerce 購物車

Adding multiple items to WooCommerce cart at once(一次將多個項目添加到 WooCommerce 購物車)
本文介紹了一次將多個項目添加到 WooCommerce 購物車的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我有要添加到購物車的不同商品的 3 個 ID.我可以使用 https://url.com/shop/cart/?add-to-cart=3001 但是當我想添加 3 個項目時,我不能這樣做.是否可以添加任何功能/腳本來將此功能添加到我的購物網站?

I have 3 IDs of the different items that I want to add to my shopping cart. I could use https://url.com/shop/cart/?add-to-cart=3001 but when I want to add 3 items I can't do it. Is there any function/script I can add to add this ability to my shopping website?

我嘗試在 add-to-cart 之后添加一個 & 并嘗試添加一個新值,但 GET 被覆蓋了對嗎?:https://url.com/shop/cart/?add-to-cart=3001&add-to-cart=2002&add-to-cart=1001

I tried to add an & after the add-to-cart and tried to add a new value but GETs get overridden right?: https://url.com/shop/cart/?add-to-cart=3001&add-to-cart=2002&add-to-cart=1001

推薦答案

我找到了答案!

只需將以下腳本添加到主題的functions.php:

Simply add the following script to your theme's functions.php:

function woocommerce_maybe_add_multiple_products_to_cart() {
// Make sure WC is installed, and add-to-cart qauery arg exists, and contains at least one comma.
if ( ! class_exists( 'WC_Form_Handler' ) || empty( $_REQUEST['add-to-cart'] ) || false === strpos( $_REQUEST['add-to-cart'], ',' ) ) {
    return;
}

// Remove WooCommerce's hook, as it's useless (doesn't handle multiple products).
remove_action( 'wp_loaded', array( 'WC_Form_Handler', 'add_to_cart_action' ), 20 );

$product_ids = explode( ',', $_REQUEST['add-to-cart'] );
$count       = count( $product_ids );
$number      = 0;

foreach ( $product_ids as $product_id ) {
    if ( ++$number === $count ) {
        // Ok, final item, let's send it back to woocommerce's add_to_cart_action method for handling.
        $_REQUEST['add-to-cart'] = $product_id;

        return WC_Form_Handler::add_to_cart_action();
    }

    $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 );

    /*
     * Sorry.. if you want non-simple products, you're on your own.
     *
     * Related: WooCommerce has set the following methods as private:
     * WC_Form_Handler::add_to_cart_handler_variable(),
     * WC_Form_Handler::add_to_cart_handler_grouped(),
     * WC_Form_Handler::add_to_cart_handler_simple()
     *
     * Why you gotta be like that WooCommerce?
     */
    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 );
    }
}
}

 // Fire before the WC_Form_Handler::add_to_cart_action callback.
 add_action( 'wp_loaded',        'woocommerce_maybe_add_multiple_products_to_cart', 15 );

然后您可以簡單地使用 http://shop.com/shop/cart/?add-to-cart=3001,3282 一次添加多個項目.在不同的 ID 之間使用逗號.

And then you can simply use http://shop.com/shop/cart/?add-to-cart=3001,3282 to add multiple items at once. Put a comma between different IDs.

感謝 dsgnwrks 的解決方案.

這篇關于一次將多個項目添加到 WooCommerce 購物車的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Cannot use #39;Object as class name as it is reserved Cake 2.2.x(不能使用 Object 作為類名,因為它是保留的 Cake 2.2.x)
Session is lost after an OAuth redirect(OAuth 重定向后會話丟失)
Pagination Sort in Cakephp 3.x(Cakephp 3.x 中的分頁排序)
CakePHP Shared core for multiple apps(CakePHP 多個應用程序的共享核心)
Login [ Auth-gt;identify() ] always false on CakePHP 3(在 CakePHP 3 上登錄 [ Auth-identify() ] 始終為 false)
Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 87 bytes)(致命錯誤:允許的內存大小為 134217728 字節已用盡(嘗試分配 87 字節))
主站蜘蛛池模板: 亚洲欧美第一视频 | 国产亚洲精品精品国产亚洲综合 | 国产精品有限公司 | 久热爱| 亚洲国产一区二区三区, | 99av成人精品国语自产拍 | 精品国产精品国产偷麻豆 | 一区二区三区欧美在线观看 | 成人av在线播放 | 精品欧美乱码久久久久久1区2区 | 操操操操操| 黄色在线免费观看视频网站 | 国产一区精品 | 在线中文字幕亚洲 | av在线黄 | 永久网站 | 久久综合一区 | 瑞克和莫蒂第五季在线观看 | 中文字幕乱码视频32 | 亚洲一级毛片 | 亚洲日韩中文字幕一区 | 成人欧美一区二区 | 精品九九九 | 99免费在线视频 | 国产一区二区三区精品久久久 | 中文字幕av在线播放 | 国产免费一区二区三区 | 日本不卡一区 | 在线视频一区二区 | 一区二区视屏 | 成人影院网站ww555久久精品 | 久久九精品 | 国产午夜精品一区二区 | 精品美女 | 国产亚洲高清视频 | 福利网址 | 蜜桃视频在线观看免费视频网站www | 夜夜草 | 国产一区 | 午夜在线视频一区二区三区 | 啪啪免费网 |