【问题标题】:Restore a discounted product price for a specific shipping method in Woocommerce在 Woocommerce 中恢复特定运输方式的折扣产品价格
【发布时间】:2018-08-25 16:18:36
【问题描述】:

我正在尝试找到以下问题的解决方案。 我一直在使用 Woocommerce 动态定价,以便为特定产品类别应用 25% 的折扣。 然后,如果运输选项为 local_pickup,我需要删除该折扣。 我想我可以调整以下代码。它在某个地方丢失了一些东西,所以我向您寻求帮助。

function discount_table(){
// Set $cat_in_cart to false
$cat_in_cart = false;

// Loop through all products in the Cart        
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {

    if ( has_term( 'category_1', 'product_cat', $cart_item['product_id'] ) ) {
        $cat_in_cart = true;
        break;
    }
}

    $total = WC()->cart->subtotal;
    $sconto_label = "";
    if($total >= 300){
        $sconto_label=15;       
    }elseif($total >= 220){
        $sconto_label=10;
    }elseif($total >= 150){
        $sconto_label=8;
    }elseif($total >= 100){
        $sconto_label=4;
    }
    $chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
    $chosen_shipping = explode(':',$chosen_methods[0]);

    if($chosen_shipping[0]=='local_pickup' && !$cat_in_cart){
        $sconto_label=25;
    }
    else if ($chosen_shipping[0]=='local_pickup' && $cat_in_cart){
        $sconto_label=25;
// there should be something here or not?
    }
    $sconto_cliente = (($total*$sconto_label)/100);
    if($sconto_label!="")
        $sconto_cliente_net = ($sconto_loison/1.1);
        WC()->cart->add_fee( "Discount ($sconto_label%)", -$sconto_cliente_net, false );
}
add_action( 'woocommerce_cart_calculate_fees','discount_table' );

有没有办法恢复购物车中已经打折的特定类别的一件或多件商品的原始价格?

【问题讨论】:

    标签: php wordpress woocommerce custom-taxonomy shipping-method


    【解决方案1】:

    要根据产品类别和特定选择的运输方式恢复购物车商品价格,请尝试以下(对于 Woocommerce 动态定价)

    add_filter('woocommerce_add_cart_item_data', 'add_default_price_as_cart_item_custom_data', 50, 3 );
    function add_default_price_as_cart_item_custom_data( $cart_item_data, $product_id, $variation_id ){
        // HERE define your product category(ies)
        $categories = array('t-shirts');
    
        if ( has_term( $categories, 'product_cat', $product_id ) ) {
            $product_id = $variation_id > 0 ? $variation_id : $product_id;
    
            // The WC_Product Object
            $product = wc_get_product($product_id);
    
            // Get product default base price
            $price = (float) $product->get_price();
    
            // Set the Product default base price as custom cart item data
            $cart_item_data['default_price'] = $price;
        }
        return $cart_item_data;
    }
    
    add_action( 'woocommerce_before_calculate_totals', 'restore_cart_item_price', 900, 1 );
    function restore_cart_item_price( $cart ) {
        if ( is_admin() && ! defined( 'DOING_AJAX' ) )
            return;
    
        if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
            return;
    
        // HERE set the targeted Chosen Shipping method
        $targeted_shipping_method = 'local_pickup';
    
        // Get the chosen shipping method
        $chosen_methods            = WC()->session->get( 'chosen_shipping_methods' );
        $chosen_shipping_method_id = explode(':', reset($chosen_methods) );
        $chosen_shipping_method    = reset($chosen_shipping_method_id);
    
        // Loop through cart items
        foreach ( $cart->get_cart() as $cart_item ) {
            if( $targeted_shipping_method == $chosen_shipping_method && isset($cart_item['default_price']) ){
                // Set back the default cart item price
                $cart_item['data']->set_price($cart_item['default_price']);
            }
        }
    }
    

    代码进入您的活动子主题(或活动主题)的 function.php 文件中。它应该可以工作。

    注意:当使用负费用(购物车折扣)时,始终会收取税款。

    【讨论】:

    • 感谢您的回复。不幸的是,激活了 Woocommerce 动态定价并在 functions.php 中激活了上面的前一段代码——我需要它作为折扣比例才能应用到购物车中——你的代码似乎不起作用。例如:一个购物车包含两件商品,一件属于无折扣类别,另一件属于折扣类别。后者保持其折扣价,3,75 欧元而不是 5 欧元 - 显示在购物车行中。
    • 感谢您的回复。您的一段代码似乎可以工作,但不能正常工作。我不认为这是你的脚本的错。我需要重新加载购物车页面才能更新购物车行和值。有没有替代方法来代替手动重新加载页面?好的,在这种情况下要计算税费。
    • @alemarengo 使用我使用的钩子,通常不会出现数据刷新问题,因为woocommerce_before_calculate_totals 是 ajax 驱动的。我已经稍微更新了我的答案代码,但我认为它不会改变什么。因此,还有其他一些问题在那里造成了问题。现在,几乎所有事情都可以根据您的技能和所花费的时间(以及预算)来完成。
    【解决方案2】:

    我挖掘了一点您的代码并进行了一些更改。感谢超级开发人员发现了一个错误!代码如下:

    function discount_table(){
    $cat_in_cart = false;
    $tot_cat = 0;
    foreach ( WC()->cart->get_cart() as $cart_item ) {
    
        if ( has_term( 'cat1', 'product_cat', $cart_item['product_id'] ) ) {
            $cat_in_cart = true;
            $tot_cat_price = $cart_item['data']->get_price();
            $tot_cat_qty = $cart_item['quantity'];
            $tot_cat +=  $tot_cat_price * $tot_cat_qty;
            //break;
    /* commented here, thanks to Michael Ramin: break will stop execution of the loop after found the first product belonging to the category; what if you have more than one? so remove break instruction */
        }
    }
        global $product;
        $total = WC()->cart->subtotal;
        $discount_label = "";
        if($total >= 300){
            $discount_label=15;     
        }elseif($total >= 220){
            $discount_label=10;
        }elseif($total >= 150){
            $discount_label=8;
        }elseif($total >= 100){
            $discount_label=4;
        }
        $chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
        $chosen_shipping = explode(':',$chosen_methods[0]);
    
        if($chosen_shipping[0]=='local_pickup'){
            $discount_label=25;
        }
        $discount_brand = ($total-$tot_cat)*$discount_label/100;
        if($discount_label!=""){
            $discount_brand_net = ($discount_brand/1.1);
            if($discount_brand_net != 0) // show discount only if != 0; it may happen if cart includes only targeted category products;
                WC()->cart->add_fee( "Discount ($discount_label%)", -$discount_brand_net, false );
        }   
    }
    add_action( 'woocommerce_cart_calculate_fees','discount_table' );
    

    【讨论】:

      猜你喜欢
      • 2021-05-18
      • 2020-01-15
      • 2021-01-29
      • 1970-01-01
      • 2020-11-08
      • 2018-07-06
      • 2020-04-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多