[英]Adding multiple products to WooCommerce cart at once with SKU and URL parameters
i found this plugin from Rémi Corson on his website : https://remicorson.com/add-woocommerce-product-to-cart-from-url-using-products-sku/我在 Rémi Corson 的网站上找到了这个插件: https ://remicorson.com/add-woocommerce-product-to-cart-from-url-using-products-sku/
that uses SKU on add to cart link paramater to work like this : https://storelink.com/?add-to-cart=2312323123 (it can only accept numbers )在添加到购物车链接参数上使用 SKU 的工作方式如下: https ://storelink.com/?add-to-cart=2312323123(它只能接受数字)
my request is i need it to accept multiple SKU like this https://storelink.com/?add-to-cart=2312,2454,5934我的要求是我需要它接受多个 SKU,例如https://storelink.com/?add-to-cart=2312,2454,5934
and add them to cart.并将它们添加到购物车。
/**
* 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' ) ); ```
I'm not sure if this will work exactly, but will get you on the right path.我不确定这是否会完全有效,但会让你走上正确的道路。 You basically create a helper method to create an array and loop through each of the sku
or post id values.您基本上创建了一个辅助方法来创建一个数组并遍历每个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.