Woocommerce 从购物车中删除商品 - PHP

Woocommerce remove item from cart - PHP

任务是让顾客已经购买的商品不可购买。 所以我的解决方案是在客户将产品添加到购物车时将其移除。

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');

代码完成了它必须做的事情。我很困惑,因为几天前我做了一个具有相同任务的代码并且它仍然有效(如果 'marked' 产品被添加到购物车,我必须从购物车中删除所有其他产品)。

信息:在 $list 中,我从 "blacklisted" 产品中获取文章 SKU 作为

 `object {["int"]=>int(*number*)}` .

我希望有人能帮助我。谢谢^^

在您的代码中:

  • 看来在您的代码中您可能忘记了session_start()或没有?
  • 要从购物车项目中获取产品,只需使用 $cart_item['data'] (它还处理产品变体)。要获取产品 sku,请直接使用 $cart_item['data']->get_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;           
        }       
    }
}

但是为什么你不使用 woocommerce_add_to_cart_validation 挂钩,而不是从购物车中删除产品……试试这个:

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;
}

代码进入您的活动子主题(或活动主题)的 function.php 文件。它应该有效。