📄 محتوای کامل
محدوده پشتیبانی:
ما نمی توانیم طبق سیاست پشتیبانی خود، پشتیبانی برای سفارشی سازی ها ارائه دهیم . اگر نیاز به سفارشی سازی یک قطعه کد یا گسترش قابلیت های آن دارید، توصیه می کنیم با یک شریک آژانس Woo یا یک توسعه دهنده WooCommerce در Codeable همکاری کنید .
کد را به فایل قالب فرزند خود functions.phpیا از طریق افزونه ای که امکان اضافه کردن توابع سفارشی را فراهم می کند، مانند افزونه Code snippets، اضافه کنید . از اضافه کردن مستقیم کد سفارشی به فایل قالب والد خودداری کنید functions.phpزیرا هنگام به روزرسانی قالب، این کد به طور کامل پاک می شود.
| /** |
| * Automatically add product to cart on visit |
| */ |
| add_action( ‘template_redirect’, ‘add_product_to_cart’ ); |
| function add_product_to_cart() { |
| if ( ! is_admin() ) { |
| $product_id = 64; //replace with your own product id |
| $found = false; |
| //check if product already in cart |
| if ( sizeof( WC()->cart->get_cart() ) > 0 ) { |
| foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) { |
| $_product = $values[‘data’]; |
| if ( $_product->get_id() == $product_id ) |
| $found = true; |
| } |
| // if product not found, add it |
| if ( ! $found ) |
| WC()->cart->add_to_cart( $product_id ); |
| } else { |
| // if no products in cart, add it |
| WC()->cart->add_to_cart( $product_id ); |
| } |
| } |
| } |
اگر می خواهید بسته به مجموع سبد خرید، محصولی به طور خودکار به سبد خرید اضافه شود، از کد زیر استفاده کنید:
| /** |
| * Add another product depending on the cart total |
| */ |
| add_action( ‘template_redirect’, ‘add_product_to_cart’ ); |
| function add_product_to_cart() { |
| if ( ! is_admin() ) { |
| global $woocommerce; |
| $product_id = 2831; //replace with your product id |
| $found = false; |
| $cart_total = 30; //replace with your cart total needed to add above item |
| if( $woocommerce->cart->total >= $cart_total ) { |
| //check if product already in cart |
| if ( sizeof( $woocommerce->cart->get_cart() ) > 0 ) { |
| foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) { |
| $_product = $values[‘data’]; |
| if ( $_product->get_id() == $product_id ) |
| $found = true; |
| } |
| // if product not found, add it |
| if ( ! $found ) |
| $woocommerce->cart->add_to_cart( $product_id ); |
| } else { |
| // if no products in cart, add it |
| $woocommerce->cart->add_to_cart( $product_id ); |
| } |
| } |
| } |
| } |