单个 Woocommerce 产品自定义变体添加到购物车按钮
Single Woocommerce product custom variations add to cart buttons
我自定义了 WoCommerce 单品页面 并仅使用这一行添加了购物车按钮
echo woocommerce_simple_add_to_cart();
但现在我需要为两种类型的变体添加两个购物车按钮。我的all属性和child是一样的。
type_of_audio 和 child 是 mac & windows.
我想创建两个自定义添加到购物车link。但我无法动态配置产品 ID。底部是代码。 (可变产品 ID 未正确获取,因此未将产品添加到购物车)
<?php if( $product->is_type( 'simple' ) ){
echo woocommerce_simple_add_to_cart();
}
elseif( $product->is_type( 'variable' ) ){
echo ('<a href="/?add-to-cart=$variation_id&attribute_pa_type_of_audio=mac">MAC BUTTON</a>');
echo ('<a href="/?add-to-cart=$variation_id&attribute_pa_type_of_audio=windows">WINDOWS BUTTON</a>');
}
?>
if ($product->is_type('simple')) {
echo woocommerce_simple_add_to_cart();
} elseif ($product->is_type('variable')) {
$children_ids = $product->get_children();
foreach ($children_ids as $value) {
$single_variation = new WC_Product_Variation($value);
echo '<a href="/?add-to-cart=$variation_id=' . $value . '&attribute_pa_type_of_audio=mac">MAC BUTTON</a>';
}
}
尝试以下操作,它会为带有正确按钮标签的每个变体显示添加到购物车按钮:
<?php
if ( $product->is_type( 'simple' ) ){
echo woocommerce_simple_add_to_cart();
}
elseif ( $product->is_type( 'variable' ) ){
// Loop through available variation data
foreach ( $product->get_available_variations() as $variation_data ) {
$url = '?add-to-cart='.$variation_data['variation_id']; // The dynamic variation ID (URL)
// Loop through variation product attributes data
foreach ( $variation_data['attributes'] as $attr_key => $term_slug ) {
// The text button
if ( $attr_key === 'attribute_pa_type_of_audio' && $term_slug === 'mac' )
{ // MAC
$button_text = __("MAC BUTTON", "woocommerce");
} elseif ( $attr_key === 'attribute_pa_type_of_audio' && $term_slug === 'windows' )
{ // WINDOWS
$button_text = __("WINDOWS BUTTON", "woocommerce");
} else
{ // OTHER (IF NEEDED)
$button_text = __("Add to cart", "woocommerce");
}
$url .= '&'.$attr_key.'='.$term_slug; // Adding the product attribute name with the term value (to Url)
}
// Displaying the custom variations add to cart buttons
if( isset($button_text))
echo '<a href="'.$url.'" class="button alt">'.$button_text.'</a> ';
}
}
?>
已测试并有效。
我自定义了 WoCommerce 单品页面 并仅使用这一行添加了购物车按钮
echo woocommerce_simple_add_to_cart();
但现在我需要为两种类型的变体添加两个购物车按钮。我的all属性和child是一样的。 type_of_audio 和 child 是 mac & windows.
我想创建两个自定义添加到购物车link。但我无法动态配置产品 ID。底部是代码。 (可变产品 ID 未正确获取,因此未将产品添加到购物车)
<?php if( $product->is_type( 'simple' ) ){
echo woocommerce_simple_add_to_cart();
}
elseif( $product->is_type( 'variable' ) ){
echo ('<a href="/?add-to-cart=$variation_id&attribute_pa_type_of_audio=mac">MAC BUTTON</a>');
echo ('<a href="/?add-to-cart=$variation_id&attribute_pa_type_of_audio=windows">WINDOWS BUTTON</a>');
}
?>
if ($product->is_type('simple')) {
echo woocommerce_simple_add_to_cart();
} elseif ($product->is_type('variable')) {
$children_ids = $product->get_children();
foreach ($children_ids as $value) {
$single_variation = new WC_Product_Variation($value);
echo '<a href="/?add-to-cart=$variation_id=' . $value . '&attribute_pa_type_of_audio=mac">MAC BUTTON</a>';
}
}
尝试以下操作,它会为带有正确按钮标签的每个变体显示添加到购物车按钮:
<?php
if ( $product->is_type( 'simple' ) ){
echo woocommerce_simple_add_to_cart();
}
elseif ( $product->is_type( 'variable' ) ){
// Loop through available variation data
foreach ( $product->get_available_variations() as $variation_data ) {
$url = '?add-to-cart='.$variation_data['variation_id']; // The dynamic variation ID (URL)
// Loop through variation product attributes data
foreach ( $variation_data['attributes'] as $attr_key => $term_slug ) {
// The text button
if ( $attr_key === 'attribute_pa_type_of_audio' && $term_slug === 'mac' )
{ // MAC
$button_text = __("MAC BUTTON", "woocommerce");
} elseif ( $attr_key === 'attribute_pa_type_of_audio' && $term_slug === 'windows' )
{ // WINDOWS
$button_text = __("WINDOWS BUTTON", "woocommerce");
} else
{ // OTHER (IF NEEDED)
$button_text = __("Add to cart", "woocommerce");
}
$url .= '&'.$attr_key.'='.$term_slug; // Adding the product attribute name with the term value (to Url)
}
// Displaying the custom variations add to cart buttons
if( isset($button_text))
echo '<a href="'.$url.'" class="button alt">'.$button_text.'</a> ';
}
}
?>
已测试并有效。