duanjian4331 2014-12-07 20:28
浏览 60
已采纳

WooCommerce以不同的价格以编程方式添加产品变体不会反映在产品页面上

Let me put it simple.

  1. We have products with color and size attributes in database
  2. The products are all variable products
  3. Variations are added for every color and "Any" size

Then we wrote a few functions to add 2 more variations to each product for the sizes 2XL and 3XL (the products are t-shirts) with a higher price!

The variations are added beautifully, however, on the front end, the price will not change when I select the size with the increased price unless I go to a product edit page and click the update button. Only then will the price to be added to the cart change upon selecting that size.

How can make the variations with the increased price reflect on the front-end without having to click the update button for each product?

  • 写回答

2条回答 默认 最新

  • duan4523 2014-12-07 22:57
    关注

    Maybe you need to run variable_product_sync() on each variable product?

    function so_run_once(){
    
        $variable_products = get_posts( array(
                'posts_per_page'=> -1,
                'post_type'     => 'product',
                'fields'        => 'ids',
                'post_status'   => 'publish',
                'tax_query' => array(
                array(
                    'taxonomy' => 'product_type',
                    'field'    => 'slug',
                    'terms'    => 'variable',
                ),
            )
        ) );
    
        if( $variable_products ) foreach( $variable_products as $product_id ){
            $_product = wc_get_product( $product_id );
            $_product->variable_product_sync();
        }
    }
    add_action( 'admin_init', 'so_run_once' );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 本题的答案是不是有问题
  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 蓝桥杯单片机第十三届第一场,整点继电器吸合,5s后断开出现了问题
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 Arcgis相交分析无法绘制一个或多个图形