【问题标题】:Woocommerce: Run PHP function on cart update [duplicate]Woocommerce:在购物车更新时运行 PHP 函数 [重复]
【发布时间】:2019-11-04 14:15:11
【问题描述】:

我想在我的 WooCommerce 购物车页面上显示一条消息,告诉我的客户,他们需要购买多少才能获得免费礼物。

我已经得到了下面的代码,但我有一个问题。

当客户更新他们的购物车或数量时,以下代码不会更新(因为页面没有重新加载)。

    <?php $e_cart = WC()->cart->cart_contents_total * 1.25;?>
    <?php $e_cart_remaining = 300 - $e_cart; ?>

    <?php
        if ( $e_cart < 300 ) {
            echo "Get a free gift, when you purchase for ${e_cart_remaining} more.";
        }?>

所以问题是,如果客户的购物车中有 250 美元的商品,则消息将显示:“再购买 50 美元即可获得免费礼物”。 (因为您将获得 300 美元的免费礼物)。但如果他们更改其中一种产品的数量,文本仍然显示 50。(因为页面没有更新

每次更新购物车时如何触发此脚本或代码块?

非常感谢。

【问题讨论】:

    标签: php wordpress woocommerce


    【解决方案1】:

    要根据购物车数量在购物车页面上显示自定义消息,请使用以下命令:

    // On cart page only
    add_action( 'woocommerce_check_cart_items', 'custom_total_item_quantity_message' );
    function custom_total_item_quantity_message() {
      $e_cart = WC()->cart->cart_contents_total * 1.25;
      $e_cart_remaining = 300 - $e_cart; 
    
        if( is_cart() &&  $e_cart < 300  ){
            wc_print_notice( sprintf( __("Get a free gift, when you purchase for %s more.", "woocommerce"), $e_cart_remaining ), 'notice' );
        }
    }
    

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

    【讨论】:

    • 如果仍有任何问题,请告诉我,我在这里为您提供帮助。 :)
    猜你喜欢
    • 2017-01-11
    • 1970-01-01
    • 1970-01-01
    • 2018-07-29
    • 1970-01-01
    • 2021-04-19
    • 2018-02-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多