[英]woocommerce shop product variations
I would like to show all products and their variations on the Wp/Woocommerce /shop/ page. 我想在Wp / Woocommerce / shop / page上显示所有产品及其变体。 (/woocommerce/archive-product.php).
(/woocommerce/archive-product.php)。 The process I'm describing below is my attempt at getting this to work.
我在下面描述的过程是我试图让它发挥作用。 Alternatives are welcome.
欢迎替代方案。
What is the norm: Product A and Product B show on the site. 什么是标准: 产品A和产品B在网站上显示。 Product A has 'ad to cart' button.
产品A有“ad to cart”按钮。 Product B has 'Select Options' button.
产品B有“选择选项”按钮。
What I'm after : Product A is a 'simple' product and shows on the shop page Product B is a 'variable' product and has 2 combinations (Black/Blue). 我追求的是 :产品A是一种“简单”产品,在商店页面上显示产品B是一种“可变”产品,有2种组合(黑色/蓝色)。 An Add to cart button adds this variation to the cart
添加到购物车按钮会将此变体添加到购物车
I would like to show 3 products on the shop page equaling 1* product A and 2* product B. 我想在商店页面上显示3个产品,相当于1 *产品A和2 *产品B.
So far I've pulled out if the product is variable some basic details about this product variation but I cannot seem to find the thumbnail? 到目前为止,如果产品变量有关此产品变化的一些基本细节,我已经退出了,但我似乎无法找到缩略图?
$postId = get_the_ID();
$args = array(
'post_type' => 'product_variation',
'post_status' => array( 'private', 'publish' ),
'numberposts' => -1,
'orderby' => 'menu_order',
'order' => 'asc',
'post_parent' => $postId
);
$variations = get_posts( $args );
//var_dump($variations);
//$p = get_post_meta($postId, '_product_attributes', true); // return pa_flavor
//var_dump($p);
if(!empty($variations)) {
foreach ($variations as $variation) {
$vari = new WC_Product_Variation($variation->ID);
$availableAttr = $vari->get_attribute('Flavour');
$flavour = get_post_meta($variation->ID, 'attribute_pa_flavour', true);
Therefore, I can get the attribute for 'flavor' and am looking for the thumbnail that is defined for each variation...? 因此,我可以获得'flavor'的属性,并且正在寻找为每个变体定义的缩略图......? Any help/pointers to where this could be are appreciated.
任何有关这可能的帮助/指示都表示赞赏。
Also [ probably my lack of understanding of Wordpress ] I cannot seem to pass/access variables in wc_get_template(). 另外[可能是我对Wordpress缺乏了解]我似乎无法在wc_get_template()中传递/访问变量。 This is probably a secondary question but still related to the fundamental issue of displaying both single and variation products on one page.
这可能是次要问题,但仍然与在一个页面上显示单个和变体产品的基本问题有关。
remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 );
add_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price_variation', 10 );
if ( ! function_exists( 'woocommerce_template_loop_price_variation' ) ) {
function woocommerce_template_loop_price_variation($prodVar=array()) {
if(empty($prodVar) || $prodVar['product_type'] == 'simple') {
wc_get_template( 'loop/price.php' );
} else {
wc_get_template( 'loop/price-variation.php' , $prodVar );
}
}
}
And in /price-variation.php (copy of price.php woocommerce base) 并在/price-variation.php(price.php woocommerce base的副本)
global $product;
var_dump($prodVar); // PRODUCED NOTICE to undefined variable???
<?php if ( $price_html = $product->get_price_html() ) : ?>
<span class="price"><?php echo $price_html; ?></span>
<?php endif; ?>
I don't want it global...? 我不希望它全球化......? or is this the only way?
或者这是唯一的方法吗?
Any help or recommendations appreciated. 任何帮助或建议表示赞赏。
$thumb_image = get_the_post_thumbnail($variable_id, $size);
You can override the product thumbnail (in functions.php) for the loop. 您可以覆盖循环的产品缩略图(在functions.php中)。 If the variation_id is globally set in case of a variation and null in case of a simple product, you can do:
如果在变体的情况下全局设置了variation_id,而在简单产品的情况下为null,则可以执行以下操作:
remove_action('woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_thumbnail', 10);
function own_woocommerce_thumbnail() {
global $product, $woocommerce, $variable_id;
$items_in_cart = array();
if ($woocommerce->cart->get_cart() && is_array($woocommerce->cart->get_cart())) {
foreach ($woocommerce->cart->get_cart() as $cart) {
$items_in_cart[] = $cart['product_id'];
}
}
$id = get_the_ID();
$in_cart = in_array($id, $items_in_cart);
$size = 'medium';
if ($variable_id) {
$thumb_image = get_the_post_thumbnail($variable_id, $size);
} else {
$thumb_image = get_the_post_thumbnail($id, $size);
}
echo '<span class="featured-image">';
echo $thumb_image;
echo '<span class="cart-loading" style="display: none">';
if ($in_cart) {
echo '<i class="fa fa-check-square-o"></i>';
} else {
echo '<i class="fa fa-spinner"></i>';
}
echo '</span></span>';
}
add_action('woocommerce_before_shop_loop_item_title', 'own_woocommerce_thumbnail', 10);
Note: This will brake pagination, since one product will be displayed multiple times on the product page. 注意:这将制动分页,因为一个产品将在产品页面上多次显示。 Note 2: You'll need to write some jquery for the hover actions to fix "add to cart"
注意2:您需要为悬停操作编写一些jquery来修复“添加到购物车”
Have you found a decent solution for this problem? 你找到了解决这个问题的合适解决方案吗?
I've posted a similar question here, which may give you more insights: Display all products and variations on the shop page, while maintaining pagination 我在这里发布了一个类似的问题,可能会为您提供更多见解: 在商店页面上显示所有产品和变体,同时保持分页
Edit: There is a plugin 'Woocommerce Show Single Variations' which works. 编辑:有一个插件'Woocommerce Show Single Variations'有效。 It used to be free, but not anymore: https://iconicwp.com/products/woocommerce-show-single-variations/
过去它是免费的,但现在不再是: https : //iconicwp.com/products/woocommerce-show-single-variations/
Paste this code in your theme functions.php 将此代码粘贴到主题functions.php中
/**
* Replace add to cart button in the loop.
*/
function iconic_change_loop_add_to_cart() {
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
add_action( 'woocommerce_after_shop_loop_item', 'iconic_template_loop_add_to_cart', 10 );
}
add_action( 'init', 'iconic_change_loop_add_to_cart', 10 );
/**
* Use single add to cart button for variable products.
*/
function iconic_template_loop_add_to_cart() {
global $product;
if ( ! $product->is_type( 'variable' ) ) {
woocommerce_template_loop_add_to_cart();
return;
}
remove_action( 'woocommerce_single_variation', 'woocommerce_single_variation_add_to_cart_button', 20 );
add_action( 'woocommerce_single_variation', 'iconic_loop_variation_add_to_cart_button', 20 );
woocommerce_template_single_add_to_cart();
}
/**
* Customise variable add to cart button for loop.
*
* Remove qty selector and simplify.
*/
function iconic_loop_variation_add_to_cart_button() {
global $product;
?>
<div class="woocommerce-variation-add-to-cart variations_button">
<button type="submit" class="single_add_to_cart_button button"><?php echo esc_html( $product->single_add_to_cart_text() ); ?></button>
<input type="hidden" name="add-to-cart" value="<?php echo absint( $product->get_id() ); ?>" />
<input type="hidden" name="product_id" value="<?php echo absint( $product->get_id() ); ?>" />
<input type="hidden" name="variation_id" class="variation_id" value="0" />
</div>
<?php
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.