【问题标题】:Prevent checkout if no coupon has been applied when certain products in cart如果购物车中的某些产品未应用优惠券,则阻止结帐
【发布时间】:2020-10-26 11:41:11
【问题描述】:

我在 WooCommerce 网站上工作,我试图限制仅在为其申请优惠券的情况下购买产品,因此如果不添加优惠券代码,则不应对其进行处理。

用户必须输入优惠券代码才能订购此特定产品(并非所有其他产品)。

我们不需要它来定位特定的优惠券以允许结帐,我们需要它来要求任何优惠券,因为对于这个特定的产品,我们有大约 150 多张优惠券。

基于Allow specific products to be purchase only if a coupon is applied in Woocommerce代码线程:

add_action( 'woocommerce_check_cart_items', 'mandatory_coupon_for_specific_items' );
function mandatory_coupon_for_specific_items() {
    $targeted_ids   = array(37); // The targeted product ids (in this array)
    $coupon_code    = 'summer2'; // The required coupon code

    $coupon_applied = in_array( strtolower($coupon_code), WC()->cart->get_applied_coupons() );

    // Loop through cart items
    foreach(WC()->cart->get_cart() as $cart_item ) {
        // Check cart item for defined product Ids and applied coupon
        if( in_array( $cart_item['product_id'], $targeted_ids ) && ! $coupon_applied ) {
            wc_clear_notices(); // Clear all other notices

            // Avoid checkout displaying an error notice
            wc_add_notice( sprintf( 'The product"%s" requires a coupon for checkout.', $cart_item['data']->get_name() ), 'error' );
            break; // stop the loop
        }
    }
}

处理方法:如果购物车中的某些产品没有使用优惠券,则阻止结帐。

【问题讨论】:

    标签: wordpress woocommerce product checkout coupon


    【解决方案1】:

    这是删除或调整一些条件的问题。例如,检查是否已使用优惠券

    • empty - 判断变量是否为空

    所以你得到:

    function action_woocommerce_check_cart_items() {
        // The targeted product ids (in this array)
        $targeted_ids = array( 813, 30 );
    
        // Get applied coupons
        $coupon_applieds = WC()->cart->get_applied_coupons();
        
        // Empty coupon applieds
        if ( empty ( $coupon_applieds ) ) {
    
            // Loop through cart items
            foreach( WC()->cart->get_cart() as $cart_item ) {
                // Check cart item for defined product Ids
                if ( in_array( $cart_item['product_id'], $targeted_ids )  ) {
                    // Clear all other notices          
                    wc_clear_notices();
    
                    // Avoid checkout displaying an error notice
                    wc_add_notice( sprintf( 'The product "%s" requires a coupon for checkout.', $cart_item['data']->get_name() ), 'error' );
                    
                    // Optional: remove proceed to checkout button
                    remove_action( 'woocommerce_proceed_to_checkout', 'woocommerce_button_proceed_to_checkout', 20 );
                    
                    // Break loop
                    break;
                }
            }
        }
    }   
    add_action( 'woocommerce_check_cart_items' , 'action_woocommerce_check_cart_items', 10, 0 );
    

    【讨论】:

    • 效果很好,但删除继续结帐按钮的部分在场景中几乎没有问题:当您创建具有特定电子邮件使用限制的特定优惠券时,使用此优惠券时会显示继续结帐这很棒,因为它无法匹配结帐步骤之前的验证电子邮件.. 在结帐时,当我们单击下订单并且我们使用与优惠券详细信息中允许的电子邮件不同的电子邮件时,它会自动删除优惠券和“下订单”按钮仍然出现。如果结帐时没有使用优惠券,它也可以隐藏吗?
    • 您可能已经知道,stackoverflow 的一般规则是一次 1 个问题。正如您将在代码中的附加注释(注释标记)中看到的那样,这是可选的,可以简单地删除。可以在新问题中请求进一步的增强,或者您可以尝试自己添加。最后,打算从你在stackoverflow上提出的问题中学习一些东西,这主要是通过自己寻找解决方案
    • 你说得对,我们已经了解了很多关于 $empty 以及如何查看优惠券的知识 .. 非常感谢您,我们非常感谢
    • 删除操作是指wc-template-hooks.php#L241。 do_action 可以在cart-totals.php#L107找到。
    猜你喜欢
    • 2021-12-28
    • 1970-01-01
    • 1970-01-01
    • 2021-03-18
    • 2018-03-24
    • 1970-01-01
    • 2017-01-11
    • 1970-01-01
    • 2017-07-14
    相关资源
    最近更新 更多