【发布时间】:2018-10-19 12:30:24
【问题描述】:
如果客户已经购买了该商品,则任务是使其无法购买。 所以我的解决方案是如果客户将产品添加到购物车中,就将其删除。
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');
进入
add_action( 'parse_reqeust', 'testtt');
代码做它必须做的事情。我很困惑,因为几天前我做了一个具有相同任务的代码并且它仍然有效(如果将“标记”产品添加到购物车,我必须从购物车中删除所有其他产品)。
信息:在 $list 中,我从“黑名单”产品中获取文章 SKU 作为
`object {["int"]=>int(*number*)}` .
我希望有人可以帮助我。谢谢^^
【问题讨论】:
标签: php wordpress session woocommerce cart