【问题标题】:WooCommerce | Display product attributes between Title and Add to cart button on product pageWooCommerce |在产品页面上的标题和添加到购物车按钮之间显示产品属性
【发布时间】:2018-11-26 03:43:15
【问题描述】:

如何在 WooCommerce 产品页面上的标题和添加到购物车按钮之间显示产品属性?

我喜欢将其放置在产品标题之后或添加到购物车按钮之前的描述区域中。

enter image description here

【问题讨论】:

    标签: woocommerce attributes product


    【解决方案1】:

    晚上好。我不是专家,但如果您知道属性的 slug,您可以使用函数 get_attributes 获取它,并使用 hook 将它们放置在您想要的任何位置。在这种情况下,属性是“pa_cantidad”,他的值是(1,5,10 Piezas,总共 1,2,3,5,10,100 Piezas),所以它可能是这样的:

    编辑:如果您希望属性“可链接”,可能需要更多代码。此外,您必须转到属性设置并激活“启用存档属性 - 如果您希望此属性在您的商店中有产品存档,请启用此选项。”,无需这段代码不起作用https://i.stack.imgur.com/KcTTI.jpg

    function atributos()
    {
        global $product,$post;
        echo "Atributos :";
    
    $attributes = $product->get_attributes();
    
            foreach ( $attributes as $attribute )
            {  
              $attribute_name = $attribute['name'];//pa_cantidad
    
            }
            $terms = wc_get_product_terms( $product->id, $attribute_name, array( 'fields' => 'all' ) );
    
            foreach( $terms as $term ){
    
            $Atributo=$term->name;
            $slug=$term->slug;
            $site_url=get_site_url();//http://localhost/psa
            $tax=substr($attribute_name,3);
            echo " <a href=".$site_url."/".$tax."/".$slug.">".$Atributo."</a> ";
            }
    }    
    add_action( 'woocommerce_before_variations_form', 'atributos', 5 );
    

    它有效,这就是它现在在我的店面主题中的外观。 https://i.stack.imgur.com/t92Bx.jpg

    如果我的回答对您有帮助,请考虑将其标记为答案。

    【讨论】:

    • 是的,它有效,但是否可以添加指向这些属性的链接?例如 1,5,10 Piezas 将链接到属性页面。
    • 您需要在问题中指定。我已经更新了我的答案。我测试了它并且它有效。如果我的回答对您有帮助,请将其标记为答案。
    猜你喜欢
    • 2018-03-05
    • 2020-12-13
    • 2015-01-15
    • 1970-01-01
    • 1970-01-01
    • 2018-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多