هادی خانزاده 103 ارسال شده در خرداد 95 گزارش بازنشر ارسال شده در خرداد 95 با عرض سلام و خسته نباشید بنده ی قالب فروشگاهی دارم طراحی میکنم الان تو قسمت سبد خریدش گیر کردم . میخوام از حالت آژاکس استفاده کنم که وقتی روی دکمه افزودن به سبد خرید کلیک میشه تعداد و ارقام سبد خرید به صورت خودکار و بدون رفرش صفحه آپدیت شن . در ضمن مثال های زیادی رو دیدم و انجام دادام ولی مقادیر رو کلا نشون نمیده. نمونه کد استفاده شده : if ( ! function_exists( 'new_cart_link_fragment' ) ) { /** * Cart Fragments * Ensure cart contents update when products are added to the cart via AJAX * * @param array $fragments Fragments to refresh via AJAX. * @return array Fragments to refresh via AJAX */ function new_cart_link_fragment( $fragments ) { global $woocommerce; ob_start(); new_cart_link(); $fragments['a.cart-contents'] = ob_get_clean(); ob_start(); new_handheld_footer_bar_cart_link(); $fragments['a.footer-cart-contents'] = ob_get_clean(); return $fragments; } } if ( ! function_exists( 'new_cart_link' ) ) { /** * Cart Link * Displayed a link to the cart including the number of items present and the cart total * * @return void * @since 1.0.0 */ function new_cart_link() { ?> <a class="cart-contents" href="<?php echo esc_url( WC()->cart->get_cart_url() ); ?>" title="<?php esc_attr_e( 'View your shopping cart', 'new' ); ?>"> <span class="amount"><?php echo wp_kses_data( WC()->cart->get_cart_subtotal() ); ?></span> <span class="count"><?php echo wp_kses_data( sprintf( _n( '%d item', '%d items', WC()->cart->get_cart_contents_count(), 'new' ), WC()->cart->get_cart_contents_count() ) );?></span> </a> <?php } } if ( ! function_exists( 'new_header_cart' ) ) { /** * Display Header Cart * * @since 1.0.0 * @uses is_woocommerce_activated() check if WooCommerce is activated * @return void */ function new_header_cart() { if ( is_woocommerce_activated() ) { if ( is_cart() ) { $class = 'current-menu-item'; } else { $class = ''; } ?> <ul class="site-header-cart menu"> <li class="<?php echo esc_attr( $class ); ?>"> <?php new_cart_link(); ?> </li> <li> <?php the_widget( 'WC_Widget_Cart', 'title=' ); ?> </li> </ul> <?php } } } ممنون میشم راهنماییم کنید خیلی ضروریه. با تشکر فراوان. لینک به ارسال
mojtabashahi 265 ارسال شده در خرداد 95 گزارش بازنشر ارسال شده در خرداد 95 درود ، کدهای مربوط به فایل add-to-cart.php : <?php /** * Custom Loop Add to Cart. * * Template with quantity and ajax. */ if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly. global $product; ?> <?php if ( ! $product->is_in_stock() ) : ?> <a href="<?php echo apply_filters( 'out_of_stock_add_to_cart_url', get_permalink( $product->id ) ); ?>" class="button"><?php echo apply_filters( 'out_of_stock_add_to_cart_text', __( 'Read More', 'woocommerce' ) ); ?></a> <?php else : ?> <?php $link = array( 'url' => '', 'label' => '', 'class' => '' ); switch ( $product->product_type ) { case "variable" : $link['url'] = apply_filters( 'variable_add_to_cart_url', get_permalink( $product->id ) ); $link['label'] = apply_filters( 'variable_add_to_cart_text', __( 'Select options', 'woocommerce' ) ); break; case "grouped" : $link['url'] = apply_filters( 'grouped_add_to_cart_url', get_permalink( $product->id ) ); $link['label'] = apply_filters( 'grouped_add_to_cart_text', __( 'View options', 'woocommerce' ) ); break; case "external" : $link['url'] = apply_filters( 'external_add_to_cart_url', get_permalink( $product->id ) ); $link['label'] = apply_filters( 'external_add_to_cart_text', __( 'Read More', 'woocommerce' ) ); break; default : if ( $product->is_purchasable() ) { $link['url'] = apply_filters( 'add_to_cart_url', esc_url( $product->add_to_cart_url() ) ); $link['label'] = apply_filters( 'add_to_cart_text', __( 'Add to cart', 'woocommerce' ) ); $link['class'] = apply_filters( 'add_to_cart_class', 'add_to_cart_button' ); } else { $link['url'] = apply_filters( 'not_purchasable_url', get_permalink( $product->id ) ); $link['label'] = apply_filters( 'not_purchasable_text', __( 'Read More', 'woocommerce' ) ); } break; } // If there is a simple product. if ( $product->product_type == 'simple' ) { ?> <form action="<?php echo esc_url( $product->add_to_cart_url() ); ?>" class="cart" method="post" enctype="multipart/form-data"> <?php // Displays the quantity box. woocommerce_quantity_input(); // Display the submit button. echo sprintf( '<button type="submit" data-product_id="%s" data-product_sku="%s" data-quantity="1" class="%s button product_type_simple">%s</button>', esc_attr( $product->id ), esc_attr( $product->get_sku() ), esc_attr( $link['class'] ), esc_html( $link['label'] ) ); ?> </form> <?php } else { echo apply_filters( 'woocommerce_loop_add_to_cart_link', sprintf('<a href="%s" rel="nofollow" data-product_id="%s" data-product_sku="%s" class="%s button product_type_%s">%s</a>', esc_url( $link['url'] ), esc_attr( $product->id ), esc_attr( $product->get_sku() ), esc_attr( $link['class'] ), esc_attr( $product->product_type ), esc_html( $link['label'] ) ), $product, $link ); } ?> <?php endif; ?> کدهای مربوط به فایل functions.php : function cs_wc_loop_add_to_cart_scripts() { if ( is_shop() || is_product_category() || is_product_tag() || is_product() ) : ?> <script> jQuery(document).ready(function($) { $(document).on( 'change', '.quantity .qty', function() { $(this).parent('.quantity').next('.add_to_cart_button').attr('data-quantity', $(this).val()); }); }); </script> <?php endif; } add_action( 'wp_footer', 'cs_wc_loop_add_to_cart_scripts' ); 1 لینک به ارسال
پست های پیشنهاد شده
لطفا برای ارسال دیدگاه وارد شوید
شما بعد از اینکه وارد حساب کاربری خود شدید می توانید دیدگاهی ارسال کنید
ورود به حساب کاربری