我在新的追加销售部分遇到问题我已经为wordpress / woocommerce网站添加了。
加售可以很好地显示在产品页面上,并且可以添加到购物车中,但产品ID会在标签后附加。
请参阅附图;
我使用的代码;
的functions.php
function add_upsells_to_cart( $cart_item_key ) {
global $woocommerce;
if ( empty( $_REQUEST['upsells'] ) || ! is_array( $_REQUEST['upsells'] ) )
return;
// Prevent loop
$upsells = $_REQUEST['upsells'];
unset( $_REQUEST['upsells'] );
// Append each upsells to product in cart
foreach( $upsells as $upsell_id ) {
$upsell_id = absint( $upsell_id );
// Add upsell into cart and set upsell_of as extra key with parent product item id
$woocommerce->cart->add_to_cart( $upsell_id, 1, '', '', array( 'upsell_of' => $cart_item_key ) );
}
}
/**
* ADDED UPSELL FUNCTION ON PRODUCT PAGE
*/
add_action( 'woocommerce_add_to_cart', 'add_upsells_to_cart', 1, 6 );
/**
* Inject upsell_of extra key cart item key when cart is loaded from session
*/
function get_cart_items_from_session( $item, $values, $key ) {
if ( array_key_exists( 'upsell_of', $values ) )
$item[ 'upsell_of' ] = $values['upsell_of'];
return $item;
}
add_filter( 'woocommerce_get_cart_item_from_session', 'get_cart_items_from_session', 1, 3 );
/**
* Remove associated upsells if product removed from cart
*/
function remove_upsells_from_cart( $cart_item_key ) {
global $woocommerce;
// Get cart
$cart = $woocommerce->cart->get_cart();
// For each item in cart, if item is upsell of deleted product, delete it
foreach( $cart as $upsell_key => $upsell )
if ( $upsell['upsell_of'] == $cart_item_key )
unset( $woocommerce->cart->cart_contents[ $upsell_key ] );
}
add_action( 'woocommerce_before_cart_item_quantity_zero', 'remove_upsells_from_cart' );
内容单product.php
<!-- .product_upsell -->
<?php
$upsells = $product->get_upsell_ids();
$prod_id = get_the_ID();
if ( sizeof( $upsells ) == 0 ) return;
$meta_query = $woocommerce->query->get_meta_query();
$args = array(
'post_type' => 'product',
'ignore_sticky_posts' => 1,
'no_found_rows' => 1,
'posts_per_page' => $posts_per_page,
'post__in' => $upsells,
'post__not_in' => array( $prod_id ),
'meta_query' => $meta_query
);
$query = new WP_Query( $args );
if ( $query->have_posts() ): ?>
<div class="product_upsells">
<h3>Customers also buy:</h3>
<ul>
<?php while ( $query->have_posts() ): $query->the_post(); ?>
<label class="upsell">
<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( the_ID() ), 'single-post-thumbnail' );?>
<img src="<?php echo $image[0]; ?>" />
<span class="upsell_<?php the_ID(); ?>"><input type="checkbox" name="upsells[]" id="upsell_<?php the_ID(); ?>" value="<?php the_ID(); ?>" /> <?php the_title(); ?>
<?php if ( get_post_meta( get_the_ID(), '_sale_price', true) ) { ?>
<span>£<?php $saleprice = get_post_meta( get_the_ID(), '_sale_price', true); echo money_format('%.2n', $saleprice) . "\n"; ?></span>
<?php } else { ?>
<span>£ <?php $price = get_post_meta( get_the_ID(), '_regular_price', true); echo $price; ?></span>
<?php } ?></span></label>
</span>
</label>
<?php endwhile; ?>
</ul>
<?php endif; ?>
</div><!-- END.product_upsell -->
答案 0 :(得分:6)
您的问题是由以下行引起的:
<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( the_ID() ), 'single-post-thumbnail' );?>
您需要将 the_ID()
替换为 get_the_ID()
。
<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ), 'single-post-thumbnail' ); ?>
目前正在发生的事情是,当您定义$image
变量时,它也会回显ID ,因为the_ID()
函数会回显get_the_ID()
的返回值。它的源代码实际上就是以下内容:
function the_ID() {
echo get_the_ID();
}
将这些问题交换出来应该可以很好地解决问题!
一般来说,WordPress有两种类型的后期值函数:
显示功能:这些通常用the_[…]()
定义,并将回显(显示)返回的值。非常适合您想要显示特定值的模板标签。实际上,他们只是做echo get_the_[…]()
。
检索(或返回)函数:这些函数通常使用get_the_[…]()
定义。这些是你想要定义变量的,因为它们只返回值而不输出它。