dongzaijiao4863 2019-04-15 12:30
浏览 81
已采纳

WooCommerce中变量产品的条件定制价格后缀

I have set the price suffix for all my products in Woocommerce (“per box” in my case). This works for variable products as well as for single products without problems.

Now I want this prefix to change to “per pallet” for some product variations of a product. I’ve read a lot and tried a lot, but I haven’t found a solution yet that could be applied to individual variants of a product. Can anyone help me?

Below is the Code I tried. There are no error messages, but it does not change the price of a specific id.

add_filter('woocommerce_available_variation', 'variation_price_custom_suffix', 10, 3 );
function variation_price_custom_suffix( $variation_data, $product, $variation ) {
    $args = array(
    'post_parent'   => get_the_ID() // get parent post-ID
);
$variations = get_posts( $args );

foreach ( $variations as $variation ) {

    // get variation ID
    $variation_ID = $variation->ID;

    // get variation price
    $variation_price = $product_variation->get_price_html();

    get_post_meta( $variation_ID , '_text_field_date_expire', true );

}
    if($variation_ID == 732) {
        $variation_data['price_html'] .= "Text after price";
    }
    return $variation_data;
}

Many thanks in advance and many greetings keved

  • 写回答

1条回答 默认 最新

  • douzuita7325 2019-04-15 15:50
    关注

    You are making it much more complicated that it should be and there are some mistakes:

    • The parent variable product is the $product function argument (variable).
    • You don't need to make a foreach loop, as you are already in it.
    • You are not using: get_post_meta( $variation_ID , '_text_field_date_expire', true );
    • $variation variable is the array of the current variation data (see here)
    • $variation['variation_id'] is the product variation ID
    • The parent variable product id(if needed) is $product->get_id()

    So 2 posible ways:

    1) Targeting all the product variations of a specific variable product:

    add_filter('woocommerce_available_variation', 'variation_price_custom_suffix', 10, 3 );
    function variation_price_custom_suffix( $variation_data, $product, $variation ) {
        // For a specific variable product ID (and all its variations)
        if( $product->get_id() == 732 ) {
    
            $date_expire = get_post_meta( $product->get_id(), '_text_field_date_expire', true );
    
            $variation_data['price_html'] .= ' ' . __("Text after price");
        }
    
        return $variation_data;
    }
    

    2) Targeting a unique product variations ID:

    add_filter('woocommerce_available_variation', 'variation_price_custom_suffix', 10, 3 );
    function variation_price_custom_suffix( $variation_data, $product, $variation ) {
        // For a specific product variation ID
        if( $variation['variation_id'] == 732 ) {
    
            $date_expire = get_post_meta( $variation['variation_id'], '_text_field_date_expire', true );
    
            $variation_data['price_html'] .= ' ' . __("Text after price");
        }
    
        return $variation_data;
    }
    

    Code goes in function.php file of your active child theme (or active theme). It should better work now, but you will need to complete it to use your custom field.

    Related: Adding custom text to the variation price in Woocommerce

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题