【发布时间】:2019-09-12 14:53:48
【问题描述】:
在我深入这个兔子洞之前......
我有一个产品属于一个基本上是其他 4 种产品的选择。它可以是精选产品的任意组合。定价基于这些产品的正常价格。
到目前为止我所做的事情
通过普通的 WooCommerce 属性界面控制。管理员输入属性use-for-custom,其值为项目的类别,即cupcakes
结果是与custom-assortment 类别中的custom-assortment 产品的关联。该产品将是cupcakes 类别中custom-assortment 产品中的任何/所有4 个选项的选项。
我在functions.php 中还有一个函数,它可以查询所有带有这个set 属性的产品,并构建一个简洁的id/title/cost 小数组。我之前是通过this post 管理的,另外
$useable = array();
foreach ($products as $product){
$useable[] = array(
'id' => $product->ID,
'name' => $product->post_title,
'desc' => $product->post_excerpt,
'cost' => floatval(wc_get_product($product->ID)->get_price())/4
//cost is 1/4 as 4 will be selected to create 1 new product
);
}
备注
它不需要将选择实际与特定产品相关联。在最终订单中显示选定的产品名称对管理员来说就足够了。但是,最好保留该引用,因为它可能会在以后带来更多功能。
目前不关心库存,或所选产品的除价格以外的任何其他属性。
想法
我已经尝试过通过 LoicTheAztec 的 this post 以编程方式创建自定义变体,但到目前为止我还没有让它们出现。我也许可以多戳几下,因为他的东西通常是正确的。
foreach ($useable as $useit){
$display = $useit['name'] . " (" . $useit['cost'] . ")";
$variation_data = array(
'attributes' => array(
'flavor-one' => $display,
'flavor-two' => $display,
'flavor-three' => $display,
'flavor-four' => $display
),
'sku' => '',
'regular_price' => $useit['cost'],
'sale_price' => ''
);
var_dump($product->get_id());
create_product_variation($product->get_id(), $variation_data);
}
或者,我可以手动创建下拉选择,并根据选择运行价格过滤器。不过,这似乎是对功能的大量手动重新创建——检查有效性、定价等。
最后的问题
有没有办法将一种产品用作另一种产品的可变产品属性?这似乎不是什么牵强附会的事情,但我无法通过 google-foo 搜索任何内容。我的 google-foo 最近不太好。
【问题讨论】:
标签: wordpress woocommerce