【问题标题】:Woocommerce remove item from cart - PHPWoocommerce 从购物车中删除商品 - PHP
【发布时间】: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


    【解决方案1】:

    在您的代码中:

    • 您的代码中似乎可能会忘记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 文件中。它应该可以工作。

    【讨论】:

    • 很抱歉这么晚才回复我在工作中遇到了一些问题,但非常感谢!你帮了我很多!
    猜你喜欢
    • 2021-07-26
    • 1970-01-01
    • 1970-01-01
    • 2015-07-05
    • 1970-01-01
    • 1970-01-01
    • 2013-10-17
    • 1970-01-01
    • 2018-07-22
    相关资源
    最近更新 更多