wordpress - 在购物车页面中添加自定义按钮以在购物车中添加特定产品
问题描述
尝试在购物车页面中添加自定义按钮。如果单击该按钮,则应添加特定产品,并且应使用 ajax 加载刷新购物车页面。我使用了下面的代码,但它正在刷新页面而不更新购物车价格。
add_filter( 'woocommerce_cart_item_price', 'customizing_cart_item_name', 10, 3 );
function customizing_cart_item_name( $product_name, $cart_item, $cart_item_key ) {
$product = $cart_item['data']; // Get the WC_Product Object
if ( $value = $product->get_meta('pro_price_extra_info') ) {
$product_name .= '<a class="gt_price" href="example.com/cart/?add-to-cart=250">Get this price</a>';
}
return $product_name;
}
请帮我写代码。提前致谢
解决方案
我刚刚测试了你的代码,它就像你提到的那样工作。您只需要添加一行来重新计算购物车总数:“WC()->cart->calculate_totals();”
根据您的代码:
add_filter( 'woocommerce_cart_item_price', 'customizing_cart_item_name', 10, 3 );
function customizing_cart_item_name( $product_name, $cart_item, $cart_item_key ) {
$product = $cart_item['data']; // Get the WC_Product Object
if ( $value = $product->get_meta('pro_price_extra_info') ) {
$product_name .= '<a class="gt_price" href="example.com/cart/?add-to-cart=250">Get this price</a>';
}
WC()->cart->calculate_totals();
return $product_name;
}
add_action( 'woocommerce_cart_collaterals', 'custom_add_to_cart_redirect' );
function custom_add_to_cart_redirect() {
echo '<button class="de-button-admin de-button-anim-4"><a href="example.com/cart/?add-to-cart=250" style="color:white">Get Your Discounted Product!</a></button>';
}
购物车页面挂钩:https ://www.tychesoftwares.com/woocommerce-cart-page-hooks-visual-guide-with-code-examples/
我希望它有效。祝你有美好的一天。
推荐阅读
- r - 如何使用 R 将变量添加到图形边距?
- python - Kivy 应用程序大小固定在全屏窗口中
- arrays - 删除列时,连接或 TextJoin 不起作用 - 解决方法是什么?
- c - Windows 需要 fflush,但 Linux 不需要
- python - EventFilter 中的问题,来自 QLineEdit 的 Alt / Shift 修饰符
- r - 从 r 中的两个独立数据库计算每个重叠的日期范围
- javascript - 在 Heroku 上部署 create-react-app 的应用程序错误
- perl - 无法在 Windows 中安装 MSWin32-x64-multi-thread perl
- azure-active-directory - 结合自定义注册和 Azure AD?
- python - 基于两种不同的损失函数进行更新,但在每个损失函数之后使用不同的优化器学习率(pytorch)?