【发布时间】:2018-05-22 00:16:13
【问题描述】:
在我的 woocommerce 商店link 中,我更改了“添加到购物车”按钮,当用户单击“添加到购物车”按钮时,购物车下拉菜单从顶部下降,除非触摸,否则我如何在 2 秒后关闭它, (平淡的主题) 有人可以帮我弄清楚我可以添加什么 js 或其他解决方案以使购物车下拉菜单在 2 秒后自行关闭?
如果有人想在添加到购物车旁边添加一个带有 +/- 的数量框,这将显示购物车下拉菜单,代码如下:享受。
<script>
// JS
function addToCartLink(evt, pid) {
var x = jQuery("#quantity_" + pid);
var y = evt.closest("div");
var qty = y.getElementsByClassName("input-text")[0].value;
const addToCartUrl = '/?wc-ajax=add_to_cart';
var xWWWFormUrlencodedData = "quantity=" + qty;
xWWWFormUrlencodedData += "&product_id=" + pid;
jQuery.post(addToCartUrl, xWWWFormUrlencodedData, {
withCredentials: true,
headers: {
'Cache-Control': 'no-cache',
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
'Pragma': 'no-cache'
}
}).success(result => {
if (result.error) {
console.warn('The product has been added to the cart despite that the result object indicates an error!');
return;
}
console.log('Success.', result);
jQuery("div.widget_shopping_cart_content").replaceWith(result.fragments["div.widget_shopping_cart_content"]);
jQuery("span.mega-menu-woo-cart-total").replaceWith(result.fragments["span.mega-menu-woo-cart-total"]);
jQuery("span.mega-menu-woo-cart-count").replaceWith(result.fragments["span.mega-menu-woo-cart-count"]);
jQuery(".header .cart-icon").replaceWith(result.fragments[".header .cart-icon"]);
jQuery(".image-icon.header-cart-icon").replaceWith(result.fragments[".image-icon.header-cart-icon"]);
jQuery(".cart-price").replaceWith(result.fragments[".cart-price"]);
jQuery("li.cart-item.has-icon.has-dropdown").addClass("current-dropdown");
});
return false;
}
</script>
在functions.php中
add_filter( 'woocommerce_loop_add_to_cart_link', 'quantity_inputs_for_woocommerce_loop_add_to_cart_link', 10, 2 );
function quantity_inputs_for_woocommerce_loop_add_to_cart_link( $html, $product ) {
if ( $product && $product->is_type( 'simple' ) && $product->is_purchasable() && $product->is_in_stock() && ! $product->is_sold_individually() && ! is_cart() ) {
$html = '<a rel="nofollow" data-product_id="'. $product->id .'" onclick="addToCartLink(this,' . $product->id .')" class="add_to_cart_button product_type_simple button primary is-flat mb-0 is-small">הוסף לסל</a>';
//$html = '<a onclick="addToCartLink(this,'. $product->id .')">הוסף לסל</a>';
$html .= '<div class="quantity buttons_added">';
$html .= '<input type="button" value="-" class="minus button is-form">';
$html .= '<input type="number" id="quantity_'. $product->id .'" class="input-text qty text" step="1" min="0" max="9999" name="quantity" value="1" title="כמות" size="4" pattern="[0-9]*" inputmode="numeric" >';
$html .= '<input type="button" value="+" class="plus button is-form">';
$html .= '</div>';
}
return $html;
}
【问题讨论】:
-
您应该需要给我们一个实时网站链接(请编辑您的问题)...
-
这里是链接link,添加一个产品到购物车,购物车会从顶部掉下来,如何添加一个2秒后关闭它的js?谢谢洛伊克:)
标签: javascript php jquery wordpress woocommerce