【问题标题】:Show free delivery badge if price is above 50$ in WooCommerce product/archive page如果在 WooCommerce 产品/存档页面中价格高于 50 美元,则显示免费送货徽章
【发布时间】:2018-10-16 01:50:03
【问题描述】:

我想在每件价格高于 50 美元的产品上展示“免费送货”徽章。 它应该在产品页面和循环中可见。

问题是,价格可能不止一个。如果您考虑变体和销售(甚至是变体的变体销售)。 所以我需要检查产品的类型,并且必须搜索最便宜的价格来计算。

目前我正在使用以下代码。 它有时工作正常。但在没有有效库存管理的产品上,它会在产品页面上产生超时,并且对存档不起作用(不显示任何消息)。 它还会产生一些关于不直接使用 ID 的通知。

我觉得这段代码不安全...有没有更好的存档方法? 我尝试了很多方法,但我不确定我是否考虑过价格、销售、库存或产品类型的所有可能性?!

<?php add_action( 'wgm_after_tax_display_single', 'wgm_after_tax_display_single_free_delivery', 10, 1 );
function wgm_after_tax_display_single_free_delivery(  ) {

    if (is_product()):
        global $post, $product;

        if ( ! $product->is_in_stock() ) return;

        $sale_price     = get_post_meta( $product->id, '_price', true);
        $regular_price  = get_post_meta( $product->id, '_regular_price', true);

        if (empty($regular_price)){ //then this is a variable product
            $available_variations = $product->get_available_variations();
            $variation_id=$available_variations[0]['variation_id'];
            $variation= new WC_Product_Variation( $variation_id );
            $regular_price = $variation ->regular_price;
            $sale_price = $variation ->sale_price;
        }

        if ( $sale_price >= 50 && !empty( $regular_price ) ) :
            echo 'free delivery!';
        else :
            echo 'NO free delivery!';
        endif;

    endif;
} ?>

【问题讨论】:

    标签: php wordpress woocommerce badge price


    【解决方案1】:

    由于您使用的是自定义钩子,因此很难对其进行真实测试(与您一样)...现在重新访问的代码应该以比您的更好的方式工作(解决错误通知):

    add_action( 'wgm_after_tax_display_single', 'wgm_after_tax_display_single_free_delivery', 10, 1 );
    function wgm_after_tax_display_single_free_delivery() {
        // On single product pages and archives pages
        if ( is_product() || is_shop() || is_product_category() || is_product_tag() ):
            global $post, $product;
    
            if ( ! $product->is_in_stock() ) return;
    
            // variable products (get min prices)
            if ( $product->is_type('variable') ) {
                $sale_price = $product->get_variation_sale_price('min');
                $regular_price = $product->get_variation_regular_price('min');
            }
            // Other products types
            else {
                $sale_price     = $product->get_sale_price();
                $regular_price  = $product->get_regular_price();
            }
    
            $price = $sale_price > 0 ? $sale_price : $regular_price;
    
            if ( $price >= 50  ) {
                echo __('free delivery!');
            } else {
                echo __('NO free delivery!');
            }
        endif;
    }
    

    代码进入您的活动子主题(或活动主题)的 function.php 文件中。它应该会更好。

    【讨论】:

    • 非常感谢!只有一个问题。如果我的产品有 3 个变体(20 美元、50 美元、100 美元),它会为产品页面上的每个变体显示 NO free delivery!。有没有办法解决这个问题?
    • @Cray 这很正常,因为我采用的是最低价格变化……我可以改变它,但你应该告诉我你想要什么。
    • 我在价格下方显示消息(在产品页面上)。因此,如果用户选择了 50 美元以上的变体,他应该会看到免费送货的消息。只有当变体低于 50 美元时,才应该有不免费送货的消息。在存档页面,最低价格是可以的。
    • @Cray 对于单个产品页面上的选定变体,这是另一回事,需要以另一种方式单独完成,当然要使用一些 jquery 代码。我在这里的答案代码基本上是为您要求的档案页面制作的。我还删除了您代码中的所有错误。
    • 好的,谢谢。我现在也会在产品页面上使用它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-10
    • 2017-03-10
    相关资源
    最近更新 更多