在Woocommerce变量产品的变体描述之前自定义显示

时间:2018-11-23 15:37:27

标签: php wordpress woocommerce product variations

我需要在woocommerce中的变体产品说明之前添加自己的价格表。我是通过自定义woocommerce的 includes目录中的class-wc-product-variable.php的 get_available_variation 函数来实现的。

我不想覆盖此插件,因为更改将在更新后丢失。

如何在不破坏woocommerce的情况下修改此功能?

public function get_available_variation( $variation ) {

    if ( is_numeric( $variation ) ) {
        $variation = wc_get_product( $variation );
    }
    if ( ! $variation instanceof WC_Product_Variation ) {
        return false;
    }
    // See if prices should be shown for each variation after selection.
    $show_variation_price = apply_filters( 'woocommerce_show_variation_price', $variation->get_price() === '' || $this->get_variation_sale_price( 'min' ) !== $this->get_variation_sale_price( 'max' ) || $this->get_variation_regular_price( 'min' ) !== $this->get_variation_regular_price( 'max' ), $this, $variation );

    return 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' => CUSTOM_PRICE_TABLE($variation) . 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
    );
}

我唯一要做的更改是在版本说明中添加“ CUSTOM_PRICE_TABLE($变量)”。

1 个答案:

答案 0 :(得分:2)

您只需要通过以下方式使用woocommerce_available_variation过滤器挂钩:

add_filter( 'woocommerce_available_variation', 'form_to_out_of_stock_product_variations', 10, 3 );
function form_to_out_of_stock_product_variations( $data, $product, $variation ) {
    if( CUSTOM_PRICE_TABLE($variation) )
        $data['variation_description'] = CUSTOM_PRICE_TABLE($variation) . $data['variation_description'],

    return $data;
}

代码进入您的活动子主题(或活动主题)的function.php文件中。应该可以。