ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

php-如何更改购物车中的可变产品价格还删除Woocommerce中删除特定产品表格的购物车

2019-11-08 17:41:40  阅读:193  来源: 互联网

标签:woocommerce wordpress php


在我的网站上,我将一个可变产品添加到购物车中.当时,另一个可变产品也添加到该购物车中,该产品为礼品产品.现在,我想将礼品可变产品价格更改为0,仅当条件满足购物车中提供礼物的产品.我也想通过单击提供礼物的产品来删除相同的两种产品形式.下面的代码对我不起作用.

add_action( 'woocommerce_before_calculate_totals', 'change_custom_price' );

function change_custom_price( $cart_object ) {
    $custom_price = 0; // This will be your custome price  
    $gift_variation_id = 2046;
    foreach ( $cart_object->cart_contents as $value ) {
        if ( $value['variation_id'] == $gift_variation_id ) {
            $value['data']->price = $custom_price;
        }
    }
}

解决方法:

可以根据此解决方案添加礼品-Buy one get one in woocommerce with out coupon code.

您可以将以下内容添加到主题的“ functions.php”中,以删除其他产品自动添加的礼品.

function remove_gift_product($cart_item_key) {
    global $woocommerce;
    $cat_in_cart = false;
    $coupon_in_cart = false;

    $autocoupon = array( 123411 ); // variation ids of products that offers gifts
    $freecoupon =  array( 2046 ); // variation ids of products that are gift coupons

    foreach ( $woocommerce->cart->cart_contents as $key => $values ) {      
        if( in_array( $values['variation_id'], $autocoupon ) ) {  
            $cat_in_cart = true;                
        }       
    }

    if ( !$cat_in_cart ) {          
        foreach ($woocommerce->cart->get_cart() as $cart_item_key => $cart_item) {
            if ( in_array( $cart_item['variation_id'], $freecoupon )) {             
                $woocommerce->cart->remove_cart_item($cart_item_key);
            }
        } 
    }
}
add_action( 'woocommerce_cart_item_removed', 'remove_gift_product' );

如果要降低礼品价格,请添加此标签.

function add_discount_price( $cart_object ) {
    global $woocommerce;
    $cat_in_cart = false;

    $autocoupon = array( 123411 ); // variation ids of products that offers gifts
    $freecoupon =  array( 2046 ); // variation ids of products that are gift coupons

    foreach ( $woocommerce->cart->cart_contents as $key => $values ) {      
        if( in_array( $values['variation_id'], $autocoupon ) ) {  
            $cat_in_cart = true;                
        }       
    }
    if ( $cat_in_cart ) {
        $custom_price = 0; // This will be your custome price     
        foreach ($woocommerce->cart->get_cart() as $cart_item_key => $cart_item) {
            if ( in_array( $cart_item['variation_id'], $freecoupon )) {
                 $cart_item['data']->set_price($custom_price);
            }        
        }
    }
}

add_action( 'woocommerce_before_calculate_totals', 'add_discount_price' );

标签:woocommerce,wordpress,php
来源: https://codeday.me/bug/20191108/2009656.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有