如何向/从WooCommerce Data-ProductVariments对象传递/获取数据?

时间:2016-12-06 作者:ameraz

向/从data-product_variations 产品单页中的属性表单。在使用WooCommerce时,我正在与产品库图像和产品变体进行一些交互。

基本上,我需要能够在woocommerce_variation_has_changed 事件

我能够在连接到的同时传递并获取数据属性single_product_large_thumbnail_sizewoocommerce_single_product_image_thumbnail_html.

我有not 能够在woocommerce_variation_has_changed 事件我所理解的(?)那是woocommerce_variation_has_changed 触发器从data-product_variations 对象插入与产品变体相关的图像标记。

综上所述,如何将数据属性添加到data-product_variations 对象在woocommerce_variation_has_changed 事件

这是对象在data-product_variations 看起来像。基本上我需要通过data-attrwoocommerce_variation_has_changed 踢进来。

data-product_variations="[{
    "variation_id": 373,
    "variation_is_visible": true,
    "variation_is_active": true,
    "is_purchasable": true,
    "display_price": 100,
    "display_regular_price": 100,
    "attributes": {
        "attribute_pa_chain-length": "80cm"
    },
    "image_src": "",
    "image_link": "",
    "image_title": "",
    "image_alt": "",
    "image_caption": "",
    "image_srcset": "",
    "image_sizes": "",
    "price_html": "<span class=\\"price\\"><span class=\\"woocommerce-Price-amount amount\\"><span class=\\"woocommerce-Price-currencySymbol\\">&pound;<\\/span>100.00<\\/span><\\/span>",
    "availability_html": "",
    "sku": "",
    "weight": " kg",
    "dimensions": "",
    "min_qty": 1,
    "max_qty": null,
    "backorders_allowed": false,
    "is_in_stock": true,
    "is_downloadable": false,
    "is_virtual": false,
    "is_sold_individually": "no",
    "variation_description": ""
}]"

2 个回复
SO网友:Michael Aro

  jQuery(document).on(\'found_variation.wc-variation-form\', \'form.variations_form\', function(event, variation_data) {
    //this is called when a valid productis found
  });
  jQuery(document).on(\'change.wc-variation-form\', \'form.variations_form\', function(event) {
    //this function is called when the user clicks or changes the dropdown
  });
您正在寻找的PHP函数是

apply_filters(
        \'woocommerce_available_variation\',
        array(
            \'attributes\'            => $variation->get_variation_attributes(),
            \'availability_html\'     => wc_get_stock_html( $variation ),
            \'backorders_allowed\'    => $variation->backorders_allowed(),
            \'dimensions\'            => $variation->get_dimensions( false ),
            \'dimensions_html\'       => wc_format_dimensions( $variation->get_dimensions( false ) ),
            \'display_price\'         => wc_get_price_to_display( $variation ),
            \'display_regular_price\' => wc_get_price_to_display( $variation, array( \'price\' => $variation->get_regular_price() ) ),
            \'image\'                 => wc_get_product_attachment_props( $variation->get_image_id() ),
            \'image_id\'              => $variation->get_image_id(),
            \'is_downloadable\'       => $variation->is_downloadable(),
            \'is_in_stock\'           => $variation->is_in_stock(),
            \'is_purchasable\'        => $variation->is_purchasable(),
            \'is_sold_individually\'  => $variation->is_sold_individually() ? \'yes\' : \'no\',
            \'is_virtual\'            => $variation->is_virtual(),
            \'max_qty\'               => 0 < $variation->get_max_purchase_quantity() ? $variation->get_max_purchase_quantity() : \'\',
            \'min_qty\'               => $variation->get_min_purchase_quantity(),
            \'price_html\'            => $show_variation_price ? \'<span class="price">\' . $variation->get_price_html() . \'</span>\' : \'\',
            \'sku\'                   => $variation->get_sku(),
            \'variation_description\' => wc_format_content( $variation->get_description() ),
            \'variation_id\'          => $variation->get_id(),
            \'variation_is_active\'   => $variation->variation_is_active(),
            \'variation_is_visible\'  => $variation->variation_is_visible(),
            \'weight\'                => $variation->get_weight(),
            \'weight_html\'           => wc_format_weight( $variation->get_weight() ),
        ),
        $this,
        $variation
    );
这是在这里找到的https://github.com/woocommerce/woocommerce/blob/master/includes/class-wc-product-variable.php#L325

WC_Product_Variable -> get_available_variation( $variation )

SO网友:Jake

很抱歉来晚了,但我今天需要类似的东西。这就是我设法绕过它的方式(可能不是最合乎逻辑和最有效的方式)。

首先,您不想使用woocommerce_variation_has_changed 事件,您需要show_variation 因为您可以在成功的变体匹配中使用变体对象。看见this answer 了解更多信息。

对于向变体对象添加数据,可以使用woocommerce_available_variation 滤器

add_filter( \'woocommerce_available_variation\', \'add_variation_data\', 10, 3 );
function add_variation_data( $array, $instance, $variation ){
    $array[\'custom_data_key\'] = get_post_meta( $array[\'variation_id\'], \'_meta_key\', true );
    return $array;
}
当调用show_variation 事件

希望这有帮助。