我正在生成我的购物车:
if( !is_wp_error( $this->getCartContentsCount() ) ) {
$cart_items = WC()->cart->get_cart();
foreach( $cart_items as $item ) { ?>
<div class="left col-md-3">
<div class="product-image"><!-- Make sure ot check if it's a gallery, if so, get its first image -->
<?php echo $item['data']->get_image(); ?>
</div>
</div>
<div class="right col-md-8">
<h4 class="product-name"><?php echo $item['data']->get_name(); ?></h4>
<div class="product-information">
<span class="product-quantity"><?php echo $item['quantity'] . 'x' ?></span>
<span class="product-price"><?php echo $item['data']->get_price_html(); ?></span>
</div>
<?php
echo apply_filters( 'woocommerce_cart_item_remove_link', sprintf(
'<a href="%s" aria-label="%s" data-product_id="%s" data-product_sku="%s">X</a>',
esc_url( wc_get_cart_remove_url( $item ) ),
__( 'Remove this item', 'woocommerce' ),
esc_attr( $item['product_id'] ),
esc_attr( $item['data']->get_sku() )
), $item );
?>
</div>
<?php }
}
正如您所看到的,我正在按照cart.php
使用“删除项目”机制的基本Woocommerce实现,但不幸的是,它不起作用。
这就是Woo生成的内容:
<a href="http://127.0.0.1/wordpress/cart/?remove_item%5Bkey%5D=6e7b33fdea3adc80ebd648fffb665bb8&remove_item%5Bproduct_id%5D=807&remove_item%5Bvariation_id%5D=0&remove_item%5Bquantity%5D=1&remove_item%5Bline_subtotal%5D=40&remove_item%5Bline_subtotal_tax%5D=0&remove_item%5Bline_total%5D=40&remove_item%5Bline_tax%5D=0&remove_item%5Bdata%5D&_wpnonce=2242057dec" aria-label="Remove this item" data-product_id="807" data-product_sku="235677r">X</a>
我检查了每个属性,确实有效!
其他症状:
如果直接访问该链接,会将我带到购物车本身(我 相信这是由于nonce)。
在点击之前和之后倾倒购物车仍然显示相同的内容 只有一个项目的购物车,所以删除机制没有 因为购物车不会改变其状态。
怎么会不起作用?
答案 0 :(得分:0)
我实际上已经开始复制cart.php
实施并根据我的需要进行定制:
ob_start();
if( !is_wp_error( $this->getCartContentsCount() ) ) { //This checks if there are any items in the cart.
foreach( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$_product = apply_filters(
'woocommerce_cart_item_product',
$cart_item['data'], $cart_item, $cart_item_key
);
$product_id = apply_filters(
'woocommerce_cart_item_product_id',
$cart_item['product_id'], $cart_item, $cart_item_key
);
if ( $_product && $_product->exists() && $cart_item['quantity'] > 0 && apply_filters( 'woocommerce_cart_item_visible', true, $cart_item, $cart_item_key ) ) {
$product_permalink = apply_filters(
'woocommerce_cart_item_permalink',
$_product->is_visible() ? $_product->get_permalink( $cart_item ) : '', $cart_item, $cart_item_key
); ?>
<div class="cart-item <?php echo esc_attr( apply_filters( 'woocommerce_cart_item_class', 'cart_item', $cart_item, $cart_item_key ) ); ?>">
<div class="left col-md-3">
<div class="product-image">
<?php
$thumbnail = apply_filters(
'woocommerce_cart_item_thumbnail',
$_product->get_image(), $cart_item, $cart_item_key
);
if ( !$product_permalink ) {
echo wp_kses_post( $thumbnail );
} else {
printf( '<a href="%s">%s</a>', esc_url( $product_permalink ), wp_kses_post( $thumbnail ) );
}
?>
</div>
</div>
<div class="right col-md-8">
<h4 class="product-name">
<?php
if ( ! $product_permalink ) {
echo wp_kses_post( apply_filters(
'woocommerce_cart_item_name',
$_product->get_name(), $cart_item, $cart_item_key ) . ' '
);
} else {
echo wp_kses_post( apply_filters(
'woocommerce_cart_item_name',
sprintf(
'<a href="%s">%s</a>',
esc_url( $product_permalink ), $_product->get_name()
), $cart_item, $cart_item_key
)
);
}
// Meta data.
echo wc_get_formatted_cart_item_data( $cart_item ); // PHPCS: XSS ok.
// Backorder notification.
if ( $_product->backorders_require_notification() && $_product->is_on_backorder( $cart_item['quantity'] ) ) {
echo wp_kses_post( apply_filters(
'woocommerce_cart_item_backorder_notification',
'<p class="backorder_notification">' . esc_html__( 'Available on backorder', 'woocommerce' ) . '</p>'
)
);
}
?>
</h4>
<div class="product-information">
<div class="product-quantity">
<?php
$product_quantity = sprintf(
'%s<input type="hidden" name="cart[%s][qty]" value="%s" />',
$cart_item['quantity'], $cart_item_key, $cart_item['quantity']
);
echo apply_filters(
'woocommerce_cart_item_quantity',
$product_quantity, $cart_item_key, $cart_item
); // PHPCS: XSS ok.
?>
</div>
<div class="product-price">
<?php
echo apply_filters(
'woocommerce_cart_item_price',
WC()->cart->get_product_price( $_product ), $cart_item, $cart_item_key ); // PHPCS: XSS ok.
?>
</div>
</div>
<div class="product-remove">
<?php
echo apply_filters( 'woocommerce_cart_item_remove_link', sprintf(
'<a href="%s" class="remove" aria-label="%s" data-product_id="%s" data-product_sku="%s">×</a>',
esc_url( wc_get_cart_remove_url( $cart_item_key ) ),
__( 'Remove this item', 'woocommerce' ),
esc_attr( $product_id ),
esc_attr( $_product->get_sku() )
), $cart_item_key );
?>
</div>
</div>
</div>
<?php
}
}
}
$markup_output = ob_get_contents();
ob_end_clean();
echo $markup_output;
答案 1 :(得分:0)
简单使用此WC_Cart::remove_cart_item( $cart_item_key );