Woo-commerce 3.0 单品价格没有正确变化 [英] Woo-commerce 3.0 single product price is not changing properly

查看:37
本文介绍了Woo-commerce 3.0 单品价格没有正确变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个名为sub_products"的新帖子类型,其中包含元标记unit_price".在为包含所有sub_products"帖子列表的每个 Woocommerce 产品分配一个新字段后,目标是根据所选的sub_products"元unit_price"更新每个产品价格.

I made a new post type named "sub_products" containing the meta tag "unit_price". After assigning a new field for every Woocommerce product containing a list of all the "sub_products" posts, the goal was to update every product price based on the selected "sub_products" meta "unit_price".

function kulcskron_edit_post( $p1, $p2 )
{
  if ( !is_admin() )
    return;

  if ( get_post_type() != 'product' )
    return;

  $sub_product_ids = $p2->get_meta( 'sub_products' );

  if ( empty($sub_product_ids) )
    return;

  $product_regular_price = 0;
  foreach ( $sub_product_ids as $id ) 
    $product_regular_price += get_post_meta( $id, 'unit_price', true );

  if ( $p1 == $product_regular_price )
    return;

  $p2->set_regular_price( $product_regular_price );
  $p2->save();
}
add_action( 'woocommerce_product_get_price', 'kulcskron_edit_post', 10, 2 );

我尝试了所有可能的钩子来完成这项工作:

I tried every possible hook to make this work:

add_action( 'the_post', 'kulcskron_edit_post', 9, 1 );
add_action( 'edit_post', 'kulcskron_edit_post', 10, 2 );
add_action( 'pre_get_posts', 'kulcskron_edit_post' );
add_action( 'save_post', 'kulcskron_edit_post' );

此代码以一种奇怪的方式更新了价格:

This code updates the price but in a strange way:

管理员单品编辑视图:价格不会立即更新,只是在我重新访问编辑屏幕之后.

Admin single product edit view: The price is not updated right away, just after I revisit the edit screen.

管理产品列表视图:每个价格都是 0.

前端产品页面浏览:显示的价格为0.

在管理视图中保存单个产品时,如何根据分配的sub_products"unit_price"帖子元更新产品价格?

How do I update the product price based on the assigned "sub_products" "unit_price" post meta when a single product is saved in admin view?

推荐答案

我设法解决了价格更新问题.我使用了 save_post 操作:

I managed to solve the price update problem. I used the save_post action:

save_post 是在创建或更新帖子或页面时触发的操作,这可能来自导入、发布/页面编辑表单、xmlrpc 或通过电子邮件发布.

save_post is an action triggered whenever a post or page is created or updated, which could be from an import, post/page edit form, xmlrpc, or post by email.

最终代码:

function kulcskron_update_product( $product_obj )
{
  if ( !is_admin() )
    return;

  if ( get_post_type() != 'product' )
    return;

  $product = wc_get_product( $product_obj );
  if ( !$product->meta_exists( 'sub_products' ) )
    return;

  $sub_product_ids = $product->get_meta( 'sub_products' );
  if ( empty($sub_product_ids) )
    return;

  _update_product_price( $product_obj, $sub_product_ids );

}
add_action( 'save_post', 'kulcskron_update_product' );

为了完整起见,这里是其余的代码:

And for the sake of completeness here is the rest of code:

function _update_product_price( $product_obj, $sub_product_ids )
{
  $product = wc_get_product( $product_obj );

  $product_regular_price = 0;
  foreach ( $sub_product_ids as $id ) 
    $product_regular_price += get_post_meta( $id, 'kulcskron_unit_price', true );

  $product->set_regular_price( $product_regular_price );
  $product->set_price( $product_regular_price );
  $product->save();
}

这样基本上所有内容都可以编辑和更新.示例:

This way basically everything can be edited and updated. Example:

function _update_product_stock( $product_obj, $sub_product_ids )
{
  $product = wc_get_product( $product_obj );

  $sub_product_stocks = array();
  foreach ( $sub_product_ids as $id ) 
    $sub_product_stocks[] = get_post_meta( $id, 'kulcskron_free_stock_quantity', true );

  $product->set_manage_stock( true );
  $product->set_stock_quantity( min($sub_product_stocks) );
  $product->save();
}

我很想发布一个链接,指向编辑产品的所有方法,但我没有代表.

I would love to post a link to all the methods to edit a product but I have no rep for that.

但遗憾的是这还不是全部...为了完全更新价格,我们需要过滤价格 HTML 以返回新更新的价格.

But sadly this is not all... In order to fully update the price, we need to filter the price HTML to return the newly updated price.

function kulcskron_price_html( $priceHtml, $product )
{
  $symbol = get_woocommerce_currency_symbol();
  $price = $product->get_regular_price();
  $html = '<span class="woocommerce-Price-amount amount">'. $price .'&nbsp;<span class="woocommerce-Price-currencySymbol">'. $symbol .'</span></span>';
  return $html;
}; 
add_filter( 'woocommerce_get_price_html', 'kulcskron_price_html', 10, 2 );

一点背景故事.我们需要通过 XML 文件同步来自外部数据库的所有产品.但有一个问题,XML 文件只包含产品部件,它们不会显示、搜索、过滤,当然也不能单独订购.没有任何 Woocommerce 内置功能满足这些要求.

A little backstory. We need to sync all the products from an external database through an XML file. But there is a catch, the XML file only contains product parts and they are not displayed, searched, filtered and certainly can't be ordered individually. None of the Woocommerce build in functionality meet these requirements.

为了解决这个问题,我注册了一个新的帖子类型,并将所有产品部件导入到这个新创建的帖子类型中.之后,我使用高级自定义字段插件为每个 WC 产品注册了一个新字段.

In order to solve this, I registered a new post type and imported all the product parts into this newly created post type. After that, I registered a new field for every WC Product with the Advanced Custom Fields plugin.

看起来像这样:新注册的带有自定义字段的自定义帖子类型

上面的代码可以根据选定的子产品轻松更新 WC 产品(例如价格).

The code above makes easy to update the WC Products (e.g. Prices) based on the selected Sub products.

这篇关于Woo-commerce 3.0 单品价格没有正确变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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