我在为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 ) );
} …Run Code Online (Sandbox Code Playgroud)