OceanWP + Woo:如何在产品名称和价格之间添加 STORE 站点默认属性值

时间:2021-04-09 18:27:16

标签: wordpress woocommerce

我在 Woo 中添加了 Product: Theme OceanWP

例如。 产品名称:Product1

属性 1:A

属性 2:B

属性 3:C

属性 4:D

在产品页面上,我看到名称产品和我在附加信息中的所有检查属性都是在 Woo 中自动生成并自动添加到单个产品页面。

在主商店页面上,我只有照片、产品名称、价格(我在 Woo 中关闭了其他选项,例如标签、类别、星星,在主站点商店中没有显示属性的选项)

所以……我只想在主商店页面中显示(以及所有滑块,滑块显示与我签入 Woo 相同的信息)只有一个属性值例如。

图片 产品名称 只有一个属性值 价格

当我添加此代码时:

add_action( 'woocommerce_after_shop_loop_item_title', 'additional_info_under_add_to_cart', 35 );
function additional_info_under_add_to_cart() {
    global $product;

    if ( $product && ( $product->has_attributes() ) ) {
        wc_display_product_attributes( $product );
    }
}

我看到了: https://prnt.sc/117f4e6

好的,我在其他产品中看到了我的 All add 属性,但是现在,如何在 Product name 和 Price 之间仅隔离属性 Model 的值,并且会修复它??

1 个答案:

答案 0 :(得分:1)

您可以使用 ocean_before_archive_product_price 钩子。

// shows the "Model" attribute between the product name and price
add_action( 'ocean_before_archive_product_price', 'add_attribute_after_shop_loop_item_title', 1 );
function add_attribute_after_shop_loop_item_title() {

    global $product;
    // if the slug of the "Model" attribute is "model" use "pa_model"
    if ( $product->get_attribute( 'pa_model' ) ) {
        echo '<p>' . $product->get_attribute( 'pa_model' ) . '</p>';
    }

}

代码已经过测试并且可以工作。将它添加到您的活动主题的functions.php。