从购物车中删除商品-PHP
这项任务是,如果客户已经购买了某项商品,则使其无法购买。 因此,我的解决方案是,如果客户将产品添加到购物车中,就将其删除从购物车中删除商品-PHP,php,wordpress,session,woocommerce,cart,Php,Wordpress,Session,Woocommerce,Cart,这项任务是,如果客户已经购买了某项商品,则使其无法购买。 因此,我的解决方案是,如果客户将产品添加到购物车中,就将其删除 add_action( 'woocommerce_add_to_cart', 'testtt'); function testtt() { $token = $_SESSION['******token']; $dataservice = *******Service::getService('DataService'); $list = $d
add_action( 'woocommerce_add_to_cart', 'testtt');
function testtt()
{
$token = $_SESSION['******token'];
$dataservice = *******Service::getService('DataService');
$list = $dataservice->getArticleBlacklist( $token );
foreach( WC()->cart->get_cart() as $cart_item_key => $cart_item )
{
//Get SKU by product_id or if available variation_id
if( $cart_item['variation_id'] != 0 )
{
$prototype = new WC_Product( $cart_item['variation_id'] );
$prod_art_id = $prototype->get_sku();
}
else
{
$prototype = new WC_Product( $cart_item['product_id'] ) ;
$prod_art_id = $prototype->get_sku();
}
//convert SKU from STRING into INTEGER
$x = intval( $prod_art_id );
//Remove product
if( $x == $list->int )
{
WC()->cart->remove_cart_item( $cart_item_key );
}
else
{
continue;
}
}
我尝试了一些不同的编码艺术
例如:
//Remove product
if( $x == $list['int'] )
{
还有很多其他的事情。。。什么都不管用。但问题是我知道它是有效的。因为如果我改变
add_action( 'woocommerce_add_to_cart', 'testtt');
进入
代码执行它必须执行的操作。我非常困惑,因为几天前我用同样的任务编写了一个代码,但它仍然有效(如果将“标记”的产品添加到购物车中,我必须从购物车中删除所有其他产品)
信息:在$list中,我从“黑名单”产品中获取文章SKU作为
`object {["int"]=>int(*number*)}` .
我希望有人能帮助我。感谢您的代码中的^ ^:
- 似乎在您的代码中,您可能忘记了
session\u start()
- 要从购物车项目获取产品,只需使用
(它还处理产品变化)。要获取产品sku,请直接使用$cart\u item['data']
$cart\u item['data']->get\u sku()
add_action( 'woocommerce_add_to_cart', 'testtt');
function testtt()
{
session_start(); // <== Missing?
$token = $_SESSION['******token'];
$dataservice = *******Service::getService('DataService');
$list = $dataservice->getArticleBlacklist( $token );
foreach( WC()->cart->get_cart() as $cart_item_key => $cart_item )
{
// Get the product SKU (even for product variations)
$sku = $cart_item['data']->get_sku();
// Convert SKU from STRING into INTEGER
$inst_sku = intval( $sku );
// Remove product
if( $inst_sku == $list->int )
{
WC()->cart->remove_cart_item( $cart_item_key );
}
else
{
continue;
}
}
}
代码进入活动子主题(或活动主题)的function.php文件。应该能用。很抱歉这么晚才回复,我在工作中遇到了一些问题,但非常感谢!你帮了我很多!
add_action( 'woocommerce_add_to_cart', 'testtt');
function testtt()
{
session_start(); // <== Missing?
$token = $_SESSION['******token'];
$dataservice = *******Service::getService('DataService');
$list = $dataservice->getArticleBlacklist( $token );
foreach( WC()->cart->get_cart() as $cart_item_key => $cart_item )
{
// Get the product SKU (even for product variations)
$sku = $cart_item['data']->get_sku();
// Convert SKU from STRING into INTEGER
$inst_sku = intval( $sku );
// Remove product
if( $inst_sku == $list->int )
{
WC()->cart->remove_cart_item( $cart_item_key );
}
else
{
continue;
}
}
}
add_filter( 'woocommerce_add_to_cart_validation', 'check_add_to_cart', 20, 3 );
function check_add_to_cart ( $passed, $product_id, $quantity ){
session_start();
$token = $_SESSION['******token'];
$dataservice = *******Service::getService('DataService');
$list = $dataservice->getArticleBlacklist( $token );
// Get sku from the product ID
$sku = get_post_meta( $product_id, '_sku', true )
// Convert SKU from STRING into INTEGER
$int_sku = intval( $sku );
// If the product is black listed
if( $int_sku == $list->int )
// Add a custom error notice and avoid add to cart
wc_add_notice( __('This product has already been bought... Try something else', 'woocommerce' ), 'error' );
$passed = false;
}
return $passed;
}