【发布时间】:2022-03-25 20:48:09
【问题描述】:
我有这个代码来显示产品何时已经在购物车中,我还需要它来显示产品的数量。
add_filter( 'woocommerce_product_add_to_cart_text', 'wcpt_modify_add_to_cart_text', 9999, 2 );
function wcpt_modify_add_to_cart_text( $text, $product ) {
// if cart is empty return 'Add to Quote'
if ( WC()->cart->is_empty() ) return 'Add to Quote';
// otherwise loop through cart items
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$product_id = apply_filters( 'woocommerce_cart_item_product_id', $cart_item['product_id'], $cart_item, $cart_item_key );
$qty = $cart_item['quantity'];
// if cart contains current product ID return its quantity
if ( $product->get_id() == $product_id ) {
return $qty . ' - Already Added';
}
}
// if the cart is not empty but the product is not in the cart...
return 'Add to Quote';
}
上面的更新代码用 Hooks ASWELL 更新 感谢@businessbloomer,上面的代码创造了奇迹,现在唯一的问题是我必须刷新页面才能看到任何内容
【问题讨论】:
-
您的更新使其更加清晰。但是:你从哪里调用这个函数?通过简码?从模板文件?通过一个特定的钩子?你为什么使用
did_action? -
@7uc1f3r,我已经编辑了我的问题,请查看说明。
标签: php wordpress woocommerce