Php 如何通过“添加到购物车”查询字符串将多个可变产品添加到购物车
当我在我的网站上点击一个链接时,我想在我的购物车中添加两个可变的产品。我在我的function.php中添加了下面的代码。这段代码只适用于简单产品,而不适用于变量产品Php 如何通过“添加到购物车”查询字符串将多个可变产品添加到购物车,php,wordpress,woocommerce,cart,Php,Wordpress,Woocommerce,Cart,当我在我的网站上点击一个链接时,我想在我的购物车中添加两个可变的产品。我在我的function.php中添加了下面的代码。这段代码只适用于简单产品,而不适用于变量产品 function woocommerce_maybe_add_multiple_products_to_cart( $url = false ) { // Make sure WC is installed, and add-to-cart qauery arg exists, and contains at least
function woocommerce_maybe_add_multiple_products_to_cart( $url = false ) {
// 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 $id_and_quantity ) {
// Check for quantities defined in curie notation (<product_id>:<product_quantity>)
// https://dsgnwrks.pro/snippets/woocommerce-allow-adding-multiple-products-to-the-cart-via-the-add-to-cart-query-string/#comment-12236
$id_and_quantity = explode( ':', $id_and_quantity );
$product_id = $id_and_quantity[0];
$_REQUEST['quantity'] = ! empty( $id_and_quantity[1] ) ? absint( $id_and_quantity[1] ) : 1;
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( $url );
}
$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->get_type(), $adding_to_cart );
// Variable product handling
if ( 'variable' === $add_to_cart_handler ) {
woo_hack_invoke_private_method( 'WC_Form_Handler', 'add_to_cart_handler_variable', $product_id );
// Grouped Products
} elseif ( 'grouped' === $add_to_cart_handler ) {
woo_hack_invoke_private_method( 'WC_Form_Handler', 'add_to_cart_handler_grouped', $product_id );
// Custom Handler
} elseif ( has_action( 'woocommerce_add_to_cart_handler_' . $add_to_cart_handler ) ){
do_action( 'woocommerce_add_to_cart_handler_' . $add_to_cart_handler, $url );
// Simple Products
} else {
woo_hack_invoke_private_method( 'WC_Form_Handler', 'add_to_cart_handler_simple', $product_id );
}
}
}
// Fire before the WC_Form_Handler::add_to_cart_action callback.
add_action( 'wp_loaded', 'woocommerce_maybe_add_multiple_products_to_cart', 15 );
/**
* Invoke class private method
*
* @since 0.1.0
*
* @param string $class_name
* @param string $methodName
*
* @return mixed
*/
function woo_hack_invoke_private_method( $class_name, $methodName ) {
if ( version_compare( phpversion(), '5.3', '<' ) ) {
throw new Exception( 'PHP version does not support ReflectionClass::setAccessible()', __LINE__ );
}
$args = func_get_args();
unset( $args[0], $args[1] );
$reflection = new ReflectionClass( $class_name );
$method = $reflection->getMethod( $methodName );
$method->setAccessible( true );
$args = array_merge( array( $class_name ), $args );
return call_user_func_array( array( $method, 'invoke' ), $args );
}
功能woocommerce\u可能会将多个产品添加到购物车($url=false){
//确保安装了WC,并且存在“添加到购物车”Qaury arg,并且至少包含一个逗号。
如果(!class_存在('WC_Form_Handler')| |为空($_请求['add-to-cart'])| | false==strpos($_请求['add-to-cart'],',')){
返回;
}
//移除WooCommerce的钩子,因为它没用(不能处理多个产品)。
删除操作('wp_loaded',数组('WC_Form_Handler','add_to_cart_action'),20);
$product_ids=分解(',',$请求['add-to-cart']);
$count=计数($product\u id);
$number=0;
foreach($id和数量的产品标识){
//检查居里符号(:)中定义的数量
// https://dsgnwrks.pro/snippets/woocommerce-allow-adding-multiple-products-to-the-cart-via-the-add-to-cart-query-string/#comment-12236
$id_和_数量=分解(“:”、$id_和_数量);
$product_id=$id_和_数量[0];
$\u请求['quantity']=!空($id\u和\u数量[1])?缺席($id\u和\u数量[1]):1;
如果(++$number===$count){
//好的,最后一项,让我们将其发送回woocommerce的add\u to\u cart\u操作方法进行处理。
$\u请求['add-to-cart']=$product\u id;
返回WC_表单_处理程序::将_添加到_购物车_操作($url);
}
$product_id=应用_过滤器('WOOCOMERCE_add_to_cart_product_id',absint($product_id));
$was\u添加到\u购物车=false;
$adding_to_cart=wc_get_product($product_id);
如果(!$将\添加到\购物车){
继续;
}
$add_to_cart_handler=apply_过滤器('woocommerce_add_to_cart_handler',$adding_to_cart->get_type(),$adding_to_cart);
//可变产品处理
if('variable'==$add\u to\u cart\u handler){
woo_hack_invoke_private_方法('WC_Form_Handler','add_to_cart_Handler_variable',$product_id);
//分组产品
}elseif('grouped'==$add\u to\u cart\u handler){
woo_hack_invoke_private_方法('WC_Form_Handler','add_to_cart_Handler_grouped',$product_id);
//自定义处理程序
}elseif(has_action('woocommerce_add_to_cart_handler.$add_to_cart_handler)){
do_操作('woocommerce_add_to_cart_handler_.$add_to_cart_handler,$url);
//简单产品
}否则{
woo_hack_invoke_private_方法('WC_Form_Handler','add_to_cart_Handler_simple',$product_id);
}
}
}
//在WC_表单_处理程序::add_to_cart_动作回调之前激发。
添加操作(“已加载工作包”、“可能会添加多个产品到购物车”,15);
/**
*调用类私有方法
*
*@自0.1.0以来
*
*@param string$class_name
*@param string$methodName
*
*@返回混合
*/
函数woo\u hack\u invoke\u private\u方法($class\u name,$methodName){
如果(version_compare(phpversion(),'5.3','基于产品id,您可以检查产品是否添加到购物车中。
下面是一个代码示例
add_action( 'woocommerce_before_cart', 'bbloomer_find_product_in_cart' );
function bbloomer_find_product_in_cart() {
$product_id = 282;
$product_cart_id = WC()->cart->generate_cart_id( $product_id );
$in_cart = WC()->cart->find_product_in_cart( $product_cart_id );
if ( $in_cart ) {
$notice = 'Product ID ' . $product_id . ' is in the Cart!';
wc_print_notice( $notice, 'notice' );
}
}
我希望这会有所帮助。根据产品id,您可以检查产品是否添加到购物车中。
下面是一个代码示例
add_action( 'woocommerce_before_cart', 'bbloomer_find_product_in_cart' );
function bbloomer_find_product_in_cart() {
$product_id = 282;
$product_cart_id = WC()->cart->generate_cart_id( $product_id );
$in_cart = WC()->cart->find_product_in_cart( $product_cart_id );
if ( $in_cart ) {
$notice = 'Product ID ' . $product_id . ' is in the Cart!';
wc_print_notice( $notice, 'notice' );
}
}
我希望它能有所帮助。这是一个向WooCommerce购物车添加多个可变产品的函数
function woocommerce_add_multiple_products_to_cart() {
if ( empty( $_REQUEST['add-to-cart-manual'] ) ) {
return;
}
global $woocommerce;
//'add-to-cart' should be in this format - product_id1|variation_id1|variation_attribute1|variation_value1,product_id2|variation_id2|variation_attribute2|variation_value2 ...
$product_var_ids = explode( ',', $_REQUEST['add-to-cart-manual'] ); //product with variation ids that needs to be added
foreach ( $product_var_ids as $product_var_id ) {
$pdt_var_temp = explode( '|', $product_var_id );
$product_id = $pdt_var_temp[0];
$variation_id = $pdt_var_temp[1];
$variation_attribute = $pdt_var_temp[2];
$variation_attribute_value = $pdt_var_temp[3];
$pdt_in_cart = false;
foreach ( $woocommerce->cart->cart_contents as $key => $values ) {
if( in_array( $values['variation_id'], $variation_id ) ) {
$pdt_in_cart = true;
}
}
if ( !$pdt_in_cart ) {
$quantity = 1;
$variation = array( $variation_attribute => $variation_attribute_value );
$woocommerce->cart->add_to_cart( $product_id, $quantity, $variation_id, $variation );
}
}
}
add_action( 'init', 'woocommerce_add_multiple_products_to_cart', 15 );
请注意您需要传递的格式-http://example.com/?add-购物车手册=44 | 45 |颜色|绿色,44 | 46 |颜色|红色
,这是格式-product|id1 | variation | variation | attribute1 | variation | variation | value1 | variation | variation | variation | id2 | variation | variation | attribute2 | variation | variation | variation将可变产品放入购物车
function woocommerce_add_multiple_products_to_cart() {
if ( empty( $_REQUEST['add-to-cart-manual'] ) ) {
return;
}
global $woocommerce;
//'add-to-cart' should be in this format - product_id1|variation_id1|variation_attribute1|variation_value1,product_id2|variation_id2|variation_attribute2|variation_value2 ...
$product_var_ids = explode( ',', $_REQUEST['add-to-cart-manual'] ); //product with variation ids that needs to be added
foreach ( $product_var_ids as $product_var_id ) {
$pdt_var_temp = explode( '|', $product_var_id );
$product_id = $pdt_var_temp[0];
$variation_id = $pdt_var_temp[1];
$variation_attribute = $pdt_var_temp[2];
$variation_attribute_value = $pdt_var_temp[3];
$pdt_in_cart = false;
foreach ( $woocommerce->cart->cart_contents as $key => $values ) {
if( in_array( $values['variation_id'], $variation_id ) ) {
$pdt_in_cart = true;
}
}
if ( !$pdt_in_cart ) {
$quantity = 1;
$variation = array( $variation_attribute => $variation_attribute_value );
$woocommerce->cart->add_to_cart( $product_id, $quantity, $variation_id, $variation );
}
}
}
add_action( 'init', 'woocommerce_add_multiple_products_to_cart', 15 );
请注意您需要传递的格式-http://example.com/?add-购物车手册=44 | 45 |颜色|绿色,44 | 46 |颜色|红色
,这是格式-product| id1 | variation | id1 | variation | attribute1 | variation | value1 | variation | variation | id2 | variation | attribute2 | variation | variation | variation | variation | variae产品进入购物车点击链接之前,我想添加两个变量的产品到购物车点击链接