【问题标题】:WooCommerce set product dimensions (width & height) based on single attributeWooCommerce 基于单个属性设置产品尺寸(宽度和高度)
【发布时间】:2020-03-22 19:46:18
【问题描述】:

The Shop 正在销售带框图片。客户可以选择框架材料尺寸(预设宽x高组合列表)。 - frame materialdimensions 都被配置为预定义的属性列表。对于尺寸,只有一些非常具体的宽度 x 高度组合可用。见例子:

然后选项 Create variations from all attributes 用于让 WooCommerce 生成这些变体的所有可能组合。所以我们最终得到一个这样的列表:

关键点是,现在我们需要 2 个标准元字段 WidthHeight 以某种方式自动填充每个变体> 基于选定的维度自定义属性。像这样:

所以基本上(没有真正的代码):

if (selected Dimensions Attribute is "30 x 20 cm") set meta $width = 30 & meta $height = 20 
if (selected Dimensions Attribute is "45 x 30 cm") set meta $width = 45 & meta $height = 30 
if (selected Dimensions Attribute is "60 x 40 cm") set meta $width = 60 & meta $height = 40 
etc.

如果发生这种情况就足够了,当变量产品被保存/更新时。该解决方案可能非常简单,因为维度的属性列表是预定义的。

我知道这可能是一种特殊情况,但我认为其他人可能也会对所需的功能感兴趣。

有人可以帮忙吗?

【问题讨论】:

    标签: wordpress woocommerce metadata dimensions


    【解决方案1】:

    以下代码在保存变化时填充宽度和高度:

    add_action("woocommerce_before_product_object_save", function ($product, $data_store) {
    
        if ("variation" !== $product->get_type()) {
            return;
        }
    
    
        $abmessung = $product->get_attribute("abmessung-3-2"); // use the text "60 x 40 cm"
        $parse = explode(" ", $abmessung);
    
        $product->set_props([
            "width" => $parse[0],
            "height" => $parse[2],
        ]);
    
    
    }, 10, 2);
    

    【讨论】:

    • Slug 是 abmessungen-3-2 而不是 abmessung-3-2(见第一张截图)。否则没关系:)
    猜你喜欢
    • 1970-01-01
    • 2018-11-08
    • 1970-01-01
    • 2022-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-02
    • 1970-01-01
    相关资源
    最近更新 更多