根据变化更改WooCommerce变量产品标题 [英] Change WooCommerce variable product title based on variations

查看:60
本文介绍了根据变化更改WooCommerce变量产品标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已在一个自定义选择框中编码了一组我的产品,这些产品根据用户的选择更改价格.它运作良好,但现在我也需要根据他们的选择来更改产品标题.

I've coded in a custom select box to a group of my products that changes the price based on the user's selection. It works well, but now I need to change the product title too based on their selection.

基本上,如果选择了选项1,则产品名称保持不变.但是,如果选择了选项2,则需要在产品标题的末尾添加"-RZ".

Basically if option 1 is chosen, the product name stays the same. But is option 2 is chosen, I need to add "-RZ" to the end of the product title.

我不确定是否可以在更改价格的woocommerce_before_calculate_totals挂钩中进行此操作,但是如果有人知道我应该使用该挂钩以及通过PHP访问当前产品标题的代码,那将是很棒的.

I'm not sure if I can do this in the 'woocommerce_before_calculate_totals' hook where I altered the prices, but if anyone knows the hook I should use and the code to access the current product's title via PHP that would be great.

如果有帮助,以下是更改价格的代码:

Here is the code that alters the price if it's helpful:

function calculate_core_fee( $cart_object ) {
    if( !WC()->session->__isset( "reload_checkout" )) {
        /* core price */

        //echo $additionalPrice;
        //$additionalPrice = 100;
        foreach ( $cart_object->cart_contents as $key => $value ) {
            $product_id = $value['product_id'];
            if( isset( $value["addOn"] ) && $value["addOn"] == $product_id) {                
                    $additionalPrice = $value['core'];
                    /* Woocommerce 3.0 + */
                    $orgPrice = floatval( $value['data']->get_price() );
                    //echo $additionalPrice;
                    //echo $orgPrice;
                    $value['data']->set_price( $orgPrice + $additionalPrice );

            }
        }
    }
}
add_action( 'woocommerce_before_calculate_totals', 'calculate_core_fee', 99 );

我知道,可能要获取名称并将其存储在SESSION变量中,以便以后使用,如果要执行此操作的钩子位于购物车,结帐页面或订购页面上,而不是在单个产品页面上.

I know may have to get the name and store it in a SESSION variable to use later if the hook to do this is on the cart, checkout, or order page rather than the single-product page.

推荐答案

是的,可以在同一钩子中实现.您可以使用类 WC_Product > get_name() set_name() 方法.但是对于价格,您应该设置并获取自定义购物车字段值以使其为(就像 $ additionalPrice = $ value ['core']; )一样.

Yes this is possible in the same hook. You can manipulate the product title with class WC_Product get_name() and set_name() methods. But as for the price, you should set and get a custom cart field value to make it (just as $additionalPrice = $value['core'];).

在这里看到一个简单的相关答案:更改WooCommerce购物车商品名称

See here a simple related answer: Changing WooCommerce cart item names

所以您可能会遇到类似的东西(只是一个假示例):

So you could have something like (just a fake example):

// Get your custom field cart value for the user selection
$userSelection = $value['user_selection'];
// Get original title
$originalTitle = $value['data']->get_name();
// Conditionally set the new item title
if($userSelection == 'option2')
    $value['data']->set_name( $originalTitle . '-RZ' );

这篇关于根据变化更改WooCommerce变量产品标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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