gpt4 book ai didi

php - Woocommerce 使用复选框追加销售

转载 作者:可可西里 更新时间:2023-11-01 12:18:15 26 4
gpt4 key购买 nike

我在为 wordpress/woocommerce 网站添加的新追加销售部分遇到问题。

加售在产品页面上显示得很好,可以添加到购物车,但产品 ID 会附加在标签之后。

请看附件照片;

preview site

Preview code

我正在使用的代码;

函数.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' );

内容单一产品.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>&pound;<?php $saleprice = get_post_meta( get_the_ID(), '_sale_price', true); echo money_format('%.2n', $saleprice) . "\n"; ?></span>
<?php } else { ?>
<span>&pound; <?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 -->

最佳答案

您的问题是由以下行引起的:

<?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有两种后置值(value)功能:

显示函数:这些函数通常用 the_[…]() 定义,并将回显(显示)返回值。非常适合您只想显示特定值的模板标签。实际上,它们只是执行 echo get_the_[…]()

检索(或返回)函数:这些通常用get_the_[…]() 定义。这些是您想要用来定义变量的变量,因为它们只返回值而不输出值。

关于php - Woocommerce 使用复选框追加销售,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46851953/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com