WooCommerce:检查产品是否具有属性 [英] WooCommerce: check if product has attribute
本文介绍了WooCommerce:检查产品是否具有属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试将文本附加到包含属性"pa_sprog"值"dansk"的产品中
I'm trying to append text to products that cointain the attribute "pa_sprog" value "dansk"
我正在使用此文章发布 这是我到目前为止所拥有的:
I am working from this post this is what I have so far:
add_action( 'woocommerce_before_add_to_cart_form', 'flag1' );
function flag1() {
$product = wc_get_product( $product_id );
// Get the product attribute value
$sprog = $product->get_attribute('pa_sprog');
// if product has attribute 'sprog' value(s)
if( $sprog="dansk" ){
echo '<div class="">"Dansk!"</div>';
} else {
// Engelsk
}
}
推荐答案
以这种方式尝试一下,我留下了代码进行调试,以便您可以详细查看WC_Product_Attribute对象.之后,您可以安全地删除此
Give it a try this way, I left the code to debug, so that you can view the WC_Product_Attribute Object in detail. Afterwards you can safely remove this
function flag1() {
global $product;
// for debug purposes, place in comment tags or delete this code
$product_attributes = $product->get_attributes();
echo '<pre>', print_r($product_attributes, 1), '</pre>';
// Get the product attribute value
$sprog = $product->get_attribute('pa_sprog');
// if product has attribute 'sprog' value(s)
if( $sprog == "dansk" ) {
echo '<div class="">Dansk!</div>';
} else {
echo '<div class="">Engelsk!</div>';
}
}
add_action( 'woocommerce_before_add_to_cart_form', 'flag1' );
这篇关于WooCommerce:检查产品是否具有属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文