WooCommerce在订单元中自定义产品名称

时间:2018-10-26 17:08:37

标签: php wordpress woocommerce

WooCommerce的旧版本曾经在每个订单元中将Product Name + (Price)保存为产品名称。但是现在他们取消了价格,只保留了名称。

我正试图把价格恢复到原来的水平。到目前为止,我一直在使用woocommerce_json_search_found_productswoocommerce_order_item_name等过滤器为产品名称添加价格,但是事实证明这工作量太大,而且可能无法证明未来

我的下一个方法是仅在每个订单元中保存正确的产品名称,以便在所有电子邮件,发票,订单详细信息页面等上正确提取该产品名称。

任何想法如何解决这个问题?只是需要一些指导,以了解使用哪些钩子才能最快获得此结果。

2 个答案:

答案 0 :(得分:0)

好,所以我想我明白了。到目前为止,这是将价格节省到产品名称的最好方法。如果有兴趣,它将保存在order_item_name表的woocommerce_order_items字段中。

add_action('woocommerce_new_order_item', 'woocommerce_new_order_item_add_price', 10, 3);

function woocommerce_new_order_item_add_price($item_id, $item, $order_id) {

    $item_type = $item->get_type();

    // We only want product order type
    if ($item_type == 'line_item') {
        $product = wc_get_product($item->get_product_id());
        $product_name = $product->get_name();
        $product_price = $product->get_price();
        $new_name = $product_name . ' ($' . $product_price . ')';
        $item->set_name($new_name);
        $item->save();  
    }
}

答案 1 :(得分:0)

我尝试将您的代码修改为我的工作。 我想在项目中提供新名称=旧名称-'_aggiunte'。 _Aggiunte是order_itemmeta中的meta_key,我想要此meta_key的meta_vlaue。 这是我的代码,但是代码返回“数组”。

function woocommerce_new_order_item_add_price($item_id, $item, $order_id) {

 $item_type = $item->get_type();    $order= wc_get_order($order_id);
 // We only want product order type
 if ($item_type == 'line_item') {
     $product = wc_get_product($item->get_product_id());
     $add =$order->get_data('_aggiunte');
     $new_name= $product_name.$add;
     $item->set_name($new_name);
     $item->save();  
    } 
}
相关问题