【问题标题】:Bulk Updating Woocommerce Variation Prices批量更新 Woocommerce 变化价格
【发布时间】:2016-05-11 21:20:14
【问题描述】:

我正在尝试使用 wordpress 之外的 php/mysql 脚本更新 woocommerce 变化价格。

我有一个脚本可以更新wp_postmeta 表中product 的一个product_variation_price_regular_price 值。

如果我有 多个 变体,则该变体的正确价格会显示在网页上 - 但价格/价格范围来自 woocommerce 的价格。 php 没有更新。

但是,如果我有只有这一个变体,表格中的价格会更新,但在呈现的网页上根本不会。

我还尝试编辑product 本身的价格。但是:我仍然在呈现的网页上得到旧价格。

基本上我现在在这些领域有相同的新价格:

product-variation -> postmeta中:_price, _regular_price

product -> postmeta中:_price, _regular_price, _min_variation_price, _max_variation_price, _min_variation_regular_price, _max_variation_regular_price

我找不到任何其他有价格的字段 - 我卡住了......

我错过了什么吗?还有其他表/字段要更新吗?

感谢您的帮助!

-编辑-

也许这会有所帮助:显然,当只有一个变体时,我的价格会以echo $product->get_price_html(); 而不是echo $value['price_html']; 呈现。那么$product->get_price_html();中使用的价格存储在哪里?

【问题讨论】:

  • 可变产品的价格在sync() 方法中确定。如果您以编程方式更改变体的价格,您还需要重新同步可变产品的价格。
  • 嗨@helgatheviking,很遗憾我的脚本不在wordpress 中。我可以在我的functions.php中添加一个过滤器吗?喜欢钩入woocommerce_get_price_html?如果是的话,你能告诉我如何触发同步吗?我不擅长使用 wordpress 钩子和过滤器...谢谢!
  • @helgatheviking 今天我挖得更深了,发现包含wp-load.php应该足以使用woocommerce钩子。我试着简单地打电话给do_action( 'woocommerce_variable_product_sync', $productId] );,但这无济于事。有什么想法吗?

标签: php mysql wordpress woocommerce


【解决方案1】:

经过一番挖掘后确定,感谢@helgatheviking 为我指明了正确的方向,我尝试了这个:

  1. 在脚本中包含 wp-load.php
  2. WC_Product_Variable::sync( $post_id );

-> 不幸的是,这对我不起作用。我之所以把它放在这里是因为我认为这是正确的方法 - 希望它可以帮助其他人。

真正帮助我的是在我的functions.php中包含这个:

// Display Price For Variable Product With Same Variations Prices
add_filter('woocommerce_available_variation', function ($value, $object = null, $variation = null) {
    if ($value['price_html'] == '') {
        $value['price_html'] = '<span class="price">' . $variation->get_price_html() . '</span>';
    }
    return $value;
}, 10, 3);

// Hook into price html
add_filter( 'woocommerce_get_price_html', 'wpa83367_price_html', 100, 2 );
function wpa83367_price_html( $price, $product ){
    WC_Product_Variable::sync( $product->id );
    $myPrice = $product->min_variation_price;
    $priceFormat = str_replace(utf8_encode('.'), ",", $myPrice);
    return '<span class="amount">from ' . $priceFormat . ' €</span>';
}

【讨论】:

  • 上述方法对您不起作用,因为您还需要删除用于某些显示器的瞬态"wc_var_prices_$product_id"
【解决方案2】:

请使用以下脚本批量更新产品变体。 点击这里查看完整代码。 https://www.pearlbells.co.uk/bulk-update-product-variation-price-woocommerce/

function getExistingProducts($updatedPrices,$skuArray) {

$loop = new WP_Query(array('post_type' => array('product', 'product_variation'), 'posts_per_page' => -1));

while ($loop->have_posts()) : $loop->the_post();

    $id = get_the_ID();
    $product = wc_get_product( $id );
    $sku = get_post_meta($id, '_sku', true);

    if( in_array( $sku, $skuArray  ) ) {

        $attributes = $product->get_attributes();
        $attributes['medium-quantity-price']['value'] = $updatedPrices[$sku][4];
        $attributes['low-quantity-price']['value'] = $updatedPrices[$sku][3];
     $attributes['v-low-quantity-price']['value'] = $updatedPrices[$sku][2];
        update_post_meta( $id,'_product_attributes',$attributes);
        echo ' Update Sku : '.$sku.' '.PHP_EOL;

    }

endwhile;

}

【讨论】:

    猜你喜欢
    • 2014-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-11
    • 1970-01-01
    • 1970-01-01
    • 2022-01-18
    • 2022-11-26
    相关资源
    最近更新 更多