即使WooCommerce中的购物车为空,也可以通过URL中的GET方法应用优惠券折扣 [英] Apply coupon discount via GET method in URL even if cart is empty in WooCommerce

查看:162
本文介绍了即使WooCommerce中的购物车为空,也可以通过URL中的GET方法应用优惠券折扣的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个插件,可以将倡导者的推荐优惠券代码发送到他们输入的电子邮件中.当观众收到此电子邮件时,我想创建一个流程,使他们可以单击电子邮件中的立即购物",然后将自动添加优惠券.

I have a plugin that sends an advocates referral coupon code to e-mails that they enter. When the audience receives this email I'd like to create a flow where they can click on 'SHOP NOW' in the e-mail and the coupon will be automatically added.

到目前为止,对于立即购买"按钮下的链接,我输入了以下内容:

As of now, for the link under the 'SHOP NOW' button I've entered the following:

websitename.biz/cart__trashed?code=DISCOUNTCODE

要处理$code,我已将其放在我的functions.php文件中:

To handle $code I've put this in my functions.php file:

add_action('woocommerce_before_cart', 'discount');
function discount( ) {
    global $woocommerce;
    $code= $_GET["code"];
   if(!empty($code)){       
    if($woocommerce->cart->add_discount($code)){ 
    echo '<div class="woocommerce_message"><strong>Applied coupon!</strong></div>';
        }
    }
}

我在这里面临的问题是:

The problem I'm facing here is:

  • 如果观众访问网站时购物车中没有任何物品,则优惠券将不适用.
  • 如果添加了一些东西并停留在那里(由于cookie),那么优惠券代码就可以完美地应用.

我相信是因为购物车是空的,所以代码不起作用.

I believe its because the cart is empty, the code does not work.

只希望在观众单击链接时应用代码.

Just want the code to be applied when the audience clicks the link.

如何使它正常工作?

推荐答案

正确的方法应该是:

  • 在购物车会话中的URL中将优惠券代码设置为自定义数据.
  • 当客户将第一个商品添加到购物车时,应用此优惠券代码中的折扣.
  • 如果客户购物车为空,请从此优惠券中删除折扣

您可以从任何网址(例如商店页面,其他档案页面,产品页面,我的帐户页面或任何现有页面)中设置任何现有优惠券代码,添加到以下现有网址:
?code=DISCOUNTCODE结束
(其中DISCOUNTCODE是您的优惠券代码名称).

You can set any existing coupon code from any Url (like shop page, other archives pages, products pages, my account pages, or any existing pages) adding to this existing url:
?code=DISCOUNTCODE at the end
(where DISCOUNTCODE is your coupon code name).

这是代码:

// Set coupon code as custom data in cart session
add_action('wp_loaded', 'add_coupon_code_to_cart_session');
function add_coupon_code_to_cart_session() {
    // Exit if no code in URL or if the coupon code is already set cart session
    if( empty( $_GET["code"] ) || WC()->session->get( 'custom_discount' ) ) return;

    if( ! WC()->session->get( 'custom_discount' ) ) {
        $coupon_code = esc_attr($_GET["code"]);
        WC()->session->set( 'custom_discount', $coupon_code );
        // If there is an existing non empty cart active session we apply the coupon
        if( ! WC()->cart->is_empty() ){
            WC()->cart->add_discount( $coupon_code );
        }
    }
}

// Add coupon code when a product is added to cart once
add_action('woocommerce_add_to_cart', 'add_coupon_code_to_cart', 10, 6 );
function add_coupon_code_to_cart( $cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data ){
    $coupon_code = WC()->session->get( 'custom_discount' );
    $applied_coupons = WC()->session->get('applied_coupons');

    if( empty($coupon_code) || in_array( $coupon_code, $applied_coupons ) ) return;

    WC()->cart->add_discount( $coupon_code );
}

// Remove coupon code when user empty his cart
add_action('woocommerce_cart_item_removed', 'check_coupon_code_cart_items_removed', 10, 6 );
function check_coupon_code_cart_items_removed( $cart_item_key, $cart ){
    $coupon_code = WC()->session->get( 'custom_discount' );

    if( $cart->has_discount( $coupon_code ) && $cart->is_empty() );
        $cart->remove_coupon( $coupon_code );
}

代码进入您的活动子主题(或活动主题)的function.php文件或任何插件文件中.

Code goes in function.php file of your active child theme (or active theme) or in any plugin file.

这已经过测试并且可以正常工作

This is tested and works

这篇关于即使WooCommerce中的购物车为空,也可以通过URL中的GET方法应用优惠券折扣的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆