mg3*_*dev 9 php wordpress woocommerce
我在为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' );
Run Code Online (Sandbox Code Playgroud)
内容单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 -->
Run Code Online (Sandbox Code Playgroud)
您的问题是由以下行引起的:
<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( the_ID() ), 'single-post-thumbnail' );?>
Run Code Online (Sandbox Code Playgroud)
您需要更换the_ID()有get_the_ID().
<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ), 'single-post-thumbnail' ); ?>
Run Code Online (Sandbox Code Playgroud)
目前正在发生的事情是,当您定义$image变量时,它也会回显ID,因为the_ID()函数回显了返回值get_the_ID().它的源代码实际上就是以下内容:
function the_ID() {
echo get_the_ID();
}
Run Code Online (Sandbox Code Playgroud)
交换它们应该可以很好地解决问题!
一般来说,WordPress有两种类型的post值函数:
显示功能:通常使用the_[…]()并显示返回值并显示它们.非常适合您想要显示特定值的模板标签.实际上,他们就是这么做的echo get_the_[…]().
检索(或返回)函数:这些函数通常用get_the_[…]().这些是您想要定义变量的变量,因为它们只返回值而不输出它.
| 归档时间: |
|
| 查看次数: |
789 次 |
| 最近记录: |