Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/230.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在Woocommerce中以编程方式应用优惠券_Php_Wordpress_Woocommerce_Discount_Coupon - Fatal编程技术网

Php 在Woocommerce中以编程方式应用优惠券

Php 在Woocommerce中以编程方式应用优惠券,php,wordpress,woocommerce,discount,coupon,Php,Wordpress,Woocommerce,Discount,Coupon,在Woocommerce中,如果购物车中的重量超过100磅,我正试图找到一种方法,对整个客户的订单提供10%的折扣。我已经实现了一半。下一步,我将寻找一种通过action/hook通过functions.php以编程方式应用优惠券代码的方法 看起来我可以使用woocommerce\u ajax\u apply\u优惠券函数来完成这个()但是我不确定如何使用它 到目前为止,我已经修改了cart.php以获得购物车中所有产品的总重量,我已经创建了一个优惠券来应用折扣(如果手动输入),并且我已经向fu

在Woocommerce中,如果购物车中的重量超过100磅,我正试图找到一种方法,对整个客户的订单提供10%的折扣。我已经实现了一半。下一步,我将寻找一种通过action/hook通过functions.php以编程方式应用优惠券代码的方法

看起来我可以使用woocommerce\u ajax\u apply\u优惠券函数来完成这个()但是我不确定如何使用它

到目前为止,我已经修改了cart.php以获得购物车中所有产品的总重量,我已经创建了一个优惠券来应用折扣(如果手动输入),并且我已经向functions.php添加了一些代码来检查重量并向用户显示一条消息

编辑:删除部分代码,完成的代码包含在下面的解决方案中


谢谢你的指导,弗雷尼。以下是工作最终结果,在满足条件时成功应用折扣券,并在不再满足条件时将其删除:

/* Mod: 10% Discount for weight greater than 100 lbs 
Works with code added to child theme: woocommerce/cart/cart.php lines 13 - 14: which gets $total_weight of cart:
        global $total_weight;
        $total_weight = $woocommerce->cart->cart_contents_weight;
*/
add_action('woocommerce_before_cart_table', 'discount_when_weight_greater_than_100');
function discount_when_weight_greater_than_100( ) {
    global $woocommerce;
    global $total_weight;
    if( $total_weight > 100 ) {
        $coupon_code = '999';
        if (!$woocommerce->cart->add_discount( sanitize_text_field( $coupon_code ))) {
            $woocommerce->show_messages();
        }
        echo '<div class="woocommerce_message"><strong>Your order is over 100 lbs so a 10% Discount has been Applied!</strong> Your total order weight is <strong>' . $total_weight . '</strong> lbs.</div>';
    }
}

/* Mod: Remove 10% Discount for weight less than or equal to 100 lbs */
add_action('woocommerce_before_cart_table', 'remove_coupon_if_weight_100_or_less');
function remove_coupon_if_weight_100_or_less( ) {
    global $woocommerce;
    global $total_weight;
    if( $total_weight <= 100 ) {
        $coupon_code = '999';
        $woocommerce->cart->get_applied_coupons();
        if (!$woocommerce->cart->remove_coupons( sanitize_text_field( $coupon_code ))) {
            $woocommerce->show_messages();
        }
        $woocommerce->cart->calculate_totals();
    }
}
/*Mod:重量超过100磅可享受10%的折扣
使用添加到子主题的代码:woocommerce/cart/cart.php第13-14行:获取购物车的$total_重量:
全球$总重量;
$total_weight=$woocommerce->cart->cart_contents_weight;
*/
添加动作(“购物车前的折扣”、“重量大于100时的折扣”);
重量大于100()时的功能折扣{
全球商业;
全球$总重量;
如果($total_weight>100){
$优惠券代码='999';
如果(!$woocommerce->cart->添加折扣(清理文本字段($优惠券代码))){
$woocommerce->show_messages();
}
echo“您的订单超过100磅,因此已申请10%的折扣!您的订单总重量为。$total_weight.”lbs;
}
}
/*Mod:重量小于或等于100磅取消10%的折扣*/
添加动作(“在购物车或桌子前放置商品”,“如果重量小于等于100,则移除优惠券”);
如果重量小于等于100,则功能删除优惠券(){
全球商业;
全球$总重量;
如果($total_weight cart->get_applicated_优惠券();
如果(!$woocommerce->cart->删除优惠券(清理优惠券文本字段($优惠券代码))){
$woocommerce->show_messages();
}
$woocommerce->cart->calculate_totals();
}
}

首先,创建折扣优惠券(通过):

然后将该优惠券应用于您的订单:

if (!$woocommerce->cart->add_discount( sanitize_text_field( $coupon_code )))
    $woocommerce->show_messages();

最后一个函数返回一个BOOL值:如果折扣成功,则返回TRUE;如果折扣因各种原因失败,则返回FALSE。

我使用了此解决方案,但在OP编写时它包含一个bug。如果用户跳过购物车预览,直接进入结帐表单,则不应用优惠券。这是我的解决方案

// Independence day 2013 coupon auto add
// Add coupon when user views cart before checkout (shipping calculation page).
add_action('woocommerce_before_cart_table', 'add_independence_day_2013_coupon_automatically');

// Add coupon when user views checkout page (would not be added otherwise, unless user views cart first).
add_action('woocommerce_before_checkout_form', 'add_independence_day_2013_coupon_automatically');

// Check if php function exists.  If it doesn't, create it.
if (!function_exists('add_independence_day_2013_coupon_automatically')) {

    function add_independence_day_2013_coupon_automatically() {

        global $woocommerce;
        $coupon_code = 'independencedaysale';
        $bc_coupon_start_date = '2013-06-30 17:00:00';
        $bc_coupon_end_date = '2013-07-08 06:59:59';

        // Only apply coupon between 12:00am on 7/1/2013 and 11:59pm on 7/7/2013 PST.
        if ((time() >= strtotime($bc_coupon_start_date)) &&
            (time() <= strtotime($bc_coupon_end_date))) {

            // If coupon has been already been added remove it.
            if ($woocommerce->cart->has_discount(sanitize_text_field($coupon_code))) {

                if (!$woocommerce->cart->remove_coupons(sanitize_text_field($coupon_code))) {

                    $woocommerce->show_messages();

                }

            }

            // Add coupon
            if (!$woocommerce->cart->add_discount(sanitize_text_field($coupon_code))) {

                $woocommerce->show_messages();

            } else {

                $woocommerce->clear_messages();
                $woocommerce->add_message('Independence day sale coupon (10%) automatically applied');
                $woocommerce->show_messages();

            }

            // Manually recalculate totals.  If you do not do this, a refresh is required before user will see updated totals when discount is removed.
            $woocommerce->cart->calculate_totals();

        } else {

            // Coupon is no longer valid, based on date.  Remove it.
            if ($woocommerce->cart->has_discount(sanitize_text_field($coupon_code))) {

                if ($woocommerce->cart->remove_coupons(sanitize_text_field($coupon_code))) {

                    $woocommerce->show_messages();

                }

                // Manually recalculate totals.  If you do not do this, a refresh is required before user will see updated totals when discount is removed.
                $woocommerce->cart->calculate_totals();

            }

        }

    }

}
//2013年独立日优惠券自动添加
//用户在结账前查看购物车时添加优惠券(发货计算页面)。
添加操作(“在购物车表之前添加商业”,“自动添加独立日”2013“优惠券”);
//当用户查看结帐页面时添加优惠券(除非用户先查看购物车,否则不会添加优惠券)。
添加操作(“签出前的woocommerce表单”,“自动添加独立日2013年优惠券”);
//检查php函数是否存在。如果不存在,则创建它。
如果(!function_存在('自动添加独立日\u 2013年\u优惠券]){
功能自动添加独立日2013优惠券(){
全球商业;
$优惠券代码='independencedaysale';
$bc_优惠券_开始日期='2013-06-30 17:00:00';
$bc_优惠券_结束日期='2013-07-08 06:59:59';
//仅在太平洋标准时间2013年7月1日上午12:00至2013年7月7日下午11:59之间使用优惠券。
如果((time()>=strottime($bc\优惠券\开始\日期))&&
(时间()购物车->有折扣(清理文本字段($优惠券\代码))){
如果(!$woocommerce->cart->删除优惠券(清理优惠券文本字段($优惠券代码))){
$woocommerce->show_messages();
}
}
//添加优惠券
如果(!$woocommerce->cart->添加折扣(清理文本字段($优惠券代码))){
$woocommerce->show_messages();
}否则{
$woocommerce->clear_messages();
$woocommerce->add_消息(“自动应用独立日销售优惠券(10%));
$woocommerce->show_messages();
}
//手动重新计算总计。如果不执行此操作,则需要刷新,用户才能在删除折扣后看到更新的总计。
$woocommerce->cart->calculate_totals();
}否则{
//基于日期的优惠券不再有效。请将其删除。
如果($woocommerce->cart->有折扣(清理文本字段($优惠券\代码))){
如果($woocommerce->cart->remove_优惠券(清理_文本_字段($优惠券_代码))){
$woocommerce->show_messages();
}
//手动重新计算总计。如果不执行此操作,则需要刷新,用户才能在删除折扣后看到更新的总计。
$woocommerce->cart->calculate_totals();
}
}
}
}
2017/2019年3年以来

一个挂钩函数中的以下压缩代码将根据显示自定义消息的购物车总重量添加折扣(优惠券)。如果客户更改购物车项目,代码将检查购物车项目,如果购物车重量低于定义的重量,则删除折扣(优惠券)

守则:

add_action('woocommerce_before_calculate_totals', 'discount_based_on_weight_threshold');
function discount_based_on_weight_threshold( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        return;

    // Your settings
    $coupon_code      = 'onweight10'; // Coupon code
    $weight_threshold = 100; // Total weight threshold

    // Initializing variables
    $total_weight     = $cart->get_cart_contents_weight();
    $applied_coupons  = $cart->get_applied_coupons();
    $coupon_code      = sanitize_text_field( $coupon_code );

    // Applying coupon
    if( ! in_array($coupon_code, $applied_coupons) && $total_weight >= $weight_threshold ){
        $cart->add_discount( $coupon_code );
        wc_clear_notices();
        wc_add_notice( sprintf(
            __("Your order is over %s so a %s Discount has been Applied! Your total order weight is %s.", "woocommerce"),
            wc_format_weight( $weight_threshold ), '10%', '<strong>' . wc_format_weight( $total_weight ) . '</strong>'
        ), "notice");
    }
    // Removing coupon
    elseif( in_array($coupon_code, $applied_coupons) && $total_weight < $weight_threshold ){
        $cart->remove_coupon( $coupon_code );
    }
}
add_action('woocommerce_在_计算_总数之前,'折扣_基于_权重_阈值');
基于重量阈值的功能折扣($cart){
if(定义了('DOING'uajax'))
返回;
如果(did_action('woocommerce_before_calculate_totals')>=2)
返回;
//你的设置
$优惠券代码='onweight10';//优惠券代码
$weight\u threshold=100;//总重量阈值
//初始化变量
add_action('woocommerce_before_calculate_totals', 'discount_based_on_weight_threshold');
function discount_based_on_weight_threshold( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        return;

    // Your settings
    $coupon_code      = 'onweight10'; // Coupon code
    $weight_threshold = 100; // Total weight threshold

    // Initializing variables
    $total_weight     = $cart->get_cart_contents_weight();
    $applied_coupons  = $cart->get_applied_coupons();
    $coupon_code      = sanitize_text_field( $coupon_code );

    // Applying coupon
    if( ! in_array($coupon_code, $applied_coupons) && $total_weight >= $weight_threshold ){
        $cart->add_discount( $coupon_code );
        wc_clear_notices();
        wc_add_notice( sprintf(
            __("Your order is over %s so a %s Discount has been Applied! Your total order weight is %s.", "woocommerce"),
            wc_format_weight( $weight_threshold ), '10%', '<strong>' . wc_format_weight( $total_weight ) . '</strong>'
        ), "notice");
    }
    // Removing coupon
    elseif( in_array($coupon_code, $applied_coupons) && $total_weight < $weight_threshold ){
        $cart->remove_coupon( $coupon_code );
    }
}