Woocommerce 显示两种变体的价格 [英] Woocommerce shows price on both variations

查看:36
本文介绍了Woocommerce 显示两种变体的价格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为客户开一家商店.现在在产品页面上,有一个下拉菜单,您可以在其中选择标准"或快递".

I'am building a shop for a client. Now on the product page, there is a dropdown where you can choose Delivery "standard" or "express.

当客户选择此项时,您可以从该产品中选择您想要的数量.

When this is chosen by the customer you can choose the amount you want from this product.

现在我在 stackoverflow 上找到了一段代码(Woocommerce 获取产品价格变化) 在下拉菜单中的金额后直接显示正确的价格.这很完美.

now i found a piece of code on stackoverflow ( Woocommerce get variation product price ) to display the correct price directly after the amount in the dropdown. this works perfect.

但现在,第一个金额变化(100(22 欧元))的价格也显示在交货下拉菜单(2 - 3 天(22 欧元).我想从交货变量中删除价格/下拉菜单.

But now , the price of the first amount variation (100 (€22) ) is also displayed on the delivery dropdown ( 2 - 3 days (€22) . And i would like to remove the price from the delivery variable / dropdown.

add_filter( 'woocommerce_variation_option_name', 'display_price_in_variation_option_name' );

function display_price_in_variation_option_name( $term ) {
    global $wpdb, $product;

    $result = $wpdb->get_col( "SELECT slug FROM {$wpdb->prefix}terms WHERE name = '$term'" );

    $term_slug = ( !empty( $result ) ) ? $result[0] : $term;


    $query = "SELECT postmeta.post_id AS product_id
                FROM {$wpdb->prefix}postmeta AS postmeta
                    LEFT JOIN {$wpdb->prefix}posts AS products ON ( products.ID = postmeta.post_id )
                WHERE postmeta.meta_key LIKE 'attribute_%'
                    AND postmeta.meta_value = '$term_slug'
                    AND products.post_parent = $product->id";

    $variation_id = $wpdb->get_col( $query );

    $parent = wp_get_post_parent_id( $variation_id[0] );

    if ( $parent > 0 ) {
        $_product = new WC_Product_Variation( $variation_id[0] );
        return $term . ' (' . woocommerce_price( $_product->get_price() ) . ')';
    }
    return $term;

}

我试图改变 $variation_id[0];到 1 , 2 , 3 ,4 和 5 ,但没有成功,所以我认为必须有另一种方法来解决它.

I tried to change the $variation_id[0]; to 1 , 2 , 3 ,4 and 5 , but no succes, so i assume there must be another way to fix it.

提前谢谢:)

推荐答案

您需要做的是在 display_price_in_variation_option_name() 函数中放入一些逻辑来检查过滤器是否应应用于特定属性.在这种情况下的一个大问题是,您在函数中唯一可以访问的是术语的名称,它不能保证是唯一的.例如,您可以在材料颜色"或包装颜色"属性中使用名为蓝色"的变体.我们需要确切地找出该术语属于哪个属性,据我所知,唯一合理的方法是修改应用此过滤器的模板,并传入一个额外的分类法"参数可以在函数内查看.

What you need to do is to put some logic in your display_price_in_variation_option_name() function to check whether the filter should apply to a particular attribute or not. One big problem in this case is that the only thing you have access to in your function is the name of the term, which is not guaranteed to be unique. For example, you could have a variation called "Blue" in both a "Material Color" or "Packaging Color" attribute. We need to find out exactly which attribute this term belongs to, and the only reasonable way to do that, as far as I can tell, is to modify the templates that apply this filter, and pass in an extra "taxonomy" parameter that you can check within the function.

例如,在模板的第 48 行

For example, on line 48 of the template

/wp-content/plugins/woocommerce/templates/single-product/add-to-cart/variable.php

/wp-content/plugins/woocommerce/templates/single-product/add-to-cart/variable.php

您会发现woocommerce_variation_option_name"过滤器的两个实例,一个在第 48 行,一个在第 54 行(从 2.3.0 版开始).您将看到这些当前传入单个参数.我们需要传入第二个参数,它是术语所属的属性的名称.幸运的是,该变量已经存在于作用域中,它被称为 $name.因此,我们需要对模板进行两次修改.首先,我们应该添加第二个 $name 变量,如下所示:

you will find two instances of the 'woocommerce_variation_option_name' filter, one on line 48 and one on line 54 (as of version 2.3.0). You will see that these currently pass in a single parameter. We need to pass in a second parameter, which is the name of the attribute the term belongs to. Luckily, that variable already exists in scope, and it's called $name. So, we need to make two modifications to the template. First, we should add the second $name variable, as such:

apply_filters( 'woocommerce_variation_option_name', $term->name )

应该变成

apply_filters( 'woocommerce_variation_option_name', $term->name, $name )

您还需要更新过滤器以接受第二个参数.所以,在你的原始代码中,这个:

You will also need to update your filter to accept a second argument. So, in your original code, this:

add_filter( 'woocommerce_variation_option_name', 'display_price_in_variation_option_name' )

应该变成:

add_filter( 'woocommerce_variation_option_name', 'display_price_in_variation_option_name', 10, 2 )

您还需要更新过滤器函数以接受第二个参数.所以这个:

And you will also need to update your filter function to accept a second argument. So this:

function display_price_in_variation_option_name( $term ) {

应该变成:

function display_price_in_variation_option_name( $term, $name = null ) {

现在您已准备好添加逻辑检查以查看该属性是否应显示价格.由于您仅表示要为运输属性隐藏它,这就是我们将使用的.首先,您需要找到要抑制的属性的全名.执行此操作的最快方法是进入 WP Admin 并转到 Products -> Attributes 部分,然后选择要取消的运输属性.然后在 URL 中,您将看到 ?taxonomy= 后跟以pa_"开头的内容 - 您想要以pa_"开头的内容(它代表产品属性).对于这个例子,假设它是pa_shipping".现在,在您的过滤器函数中,您将执行以下操作:

Now you are ready to add the logical check to see if the attribute is one that should display the price. Since you only indicated you want to hide it for the shipping attribute, that's what we'll use. First you will need to find the full name of the attribute you're looking to suppress. The quickest way to do this is to go into the WP Admin and go to the Products -> Attributes section and select the shipping attribute that you want to suppress. Then in the URL you will see ?taxonomy= followed by something beginning with "pa_" - you want the thing that starts with "pa_" (it stands for product attribute). For this example, let's say that it's "pa_shipping". Now, in your filter function, you'll do the following:

function display_price_in_variation_option_name( $term, $name = null ) {

if($name == 'pa_shipping') {
  return $term;
}

global $wpdb, $product;

// the rest of your original code

我已经测试过了,它似乎工作得很好.还有一个我可以描述的 JavaScript 选项,但它不会那么健壮(尽管它不需要您更改模板文件).

I've tested this out and it seems to work pretty well. There is also a JavaScript option that I could describe, but that would not be as robust (though it would not require you to alter template files).

这篇关于Woocommerce 显示两种变体的价格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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