簡體   English   中英

使用 SKU 和 URL 參數一次將多個產品添加到 WooCommerce 購物車

[英]Adding multiple products to WooCommerce cart at once with SKU and URL parameters

我在 Rémi Corson 的網站上找到了這個插件: https ://remicorson.com/add-woocommerce-product-to-cart-from-url-using-products-sku/

在添加到購物車鏈接參數上使用 SKU 的工作方式如下: https ://storelink.com/?add-to-cart=2312323123(它只能接受數字)

我的要求是我需要它接受多個 SKU,例如https://storelink.com/?add-to-cart=2312,2454,5934

並將它們添加到購物車。

/**
 * Plugin Name: WooCommerce: Add Product to Cart by SKU
 * Plugin URI: http://remicorson.com
 * Description: Just a demo!
 * Version: 1.0
 * Author: Remi Corson
 * Author URI: http://remicorson.com/
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly
}



/**
 * WC Product Add to Cart by SKU class
 */
class WC_Add_to_Cart_by_SKU {

    /**
     * Constructor
     */
    public function __construct() {

        define( 'WC_ADD_TO_CART_BY_SKU_VERSION', '1.0' );
        define( 'WC_ADD_TO_CART_BY_SKU_PATH', untrailingslashit( plugin_dir_path( __FILE__ ) ) );
        define( 'WC_ADD_TO_CART_BY_SKU_PLUGIN_URL', untrailingslashit( plugins_url( basename( plugin_dir_path( __FILE__ ) ), basename( __FILE__ ) ) ) );
    }
    
    /**
     * get_product_id_by_product_sku()
     * 
     * Return product ID from product SKU
     */
    public function get_product_id_by_product_sku( $add_to_cart ) {
    
        global $wpdb;
              
        $product_id = $wpdb->get_var( $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta WHERE meta_key='_sku' AND meta_value='%s' LIMIT 1", $add_to_cart ) );
              
        if ( $product_id ) return $product_id;
                
        return $add_to_cart;
    
    }
    
}

add_filter( 'woocommerce_add_to_cart_product_id', array( new WC_Add_to_Cart_by_SKU(), 'get_product_id_by_product_sku' ) ); ```

我不確定這是否會完全有效,但會讓你走上正確的道路。 您基本上創建了一個輔助方法來創建一個數組並遍歷每個sku或 post id 值。

/**
 * WC Product Add to Cart by SKU class
 */
class WC_Add_to_Cart_by_SKU {

    /**
     * Constructor
     */
    public function __construct() {

        define( 'WC_ADD_TO_CART_BY_SKU_VERSION', '1.0' );
        define( 'WC_ADD_TO_CART_BY_SKU_PATH', untrailingslashit( plugin_dir_path( __FILE__ ) ) );
        define( 'WC_ADD_TO_CART_BY_SKU_PLUGIN_URL', untrailingslashit( plugins_url( basename( plugin_dir_path( __FILE__ ) ), basename( __FILE__ ) ) ) );
    }


public function wpso_72553517_add_multiple_products( $add_to_cart ) {
    // If a comma isn't found, send to the method immediately
    if ( ! strpos( ',', $add_to_cart ) ) :
        $this->get_product_id_by_product_sku( $add_to_cart );
    endif;
    
    // Convert the product ids to an array.
    $product_ids = explode( ',', $add_to_cart );
    
    // Loop through each product sku/id 
    foreach ( $product_ids as $product_id ) :
        $this->get_product_id_by_product_sku( $product_id );
    endforeach;
}

public function get_product_id_by_product_sku( $add_to_cart ) {

    global $wpdb;

    $product_id = $wpdb->get_var( $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta WHERE meta_key='_sku' AND meta_value='%s' LIMIT 1", $add_to_cart ) );

    if ( $product_id ) {
        return $product_id;
    }

    return $add_to_cart;

   }
}

add_filter( 'woocommerce_add_to_cart_product_id', array( new WC_Add_to_Cart_by_SKU(), 'wpso_72553517_add_multiple_products' ) );

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM