WooCommerce Custom product type - Multiple add to cart sections issue

二次信任 提交于 2020-01-02 07:15:35

问题


I've created a custom product type in woocommerce called Booking.

Here is the code that I have:

class WC_Product_Booking extends WC_Product{
    /**
     * __construct function.
     *
     * @access public
     * @param mixed $product
     */
    public function __construct( $product ) {
        $this->product_type = 'booking';
        $this->supports[]   = 'ajax_add_to_cart';
        parent::__construct( $product );
        add_action('woocommerce_booking_add_to_cart', array($this, 'add_to_cart'),30);
    }

    private function show_pricing_fields(){
      ?><script type='text/javascript'>
        jQuery( document ).ready( function() {
          jQuery( '.options_group.pricing' ).addClass( 'show_if_booking' ).show();
        });
      </script><?php
    }
    /**
     * Display the add to cart button (same as for simple product)
     */
    public function add_to_cart() {
      wc_get_template( 'single-product/add-to-cart/simple.php' );
    }
}

Right now the only problem is that the add to cart section on the product page is displayed 6 times andI cant figure out why.

How can I solve this issue?

Thanks


@Update … Solution (as LoicTheAztec put me on the right track):

I've found a solution to this problem, using this code

if (! function_exists( 'woocommerce_booking_add_to_cart' ) ) {

  /**
  * Output the simple product add to cart area.
  *
  * @subpackage Product
  */

  function booking_add_to_cart() {
    wc_get_template( 'single-product/add-to-cart/simple.php' );
  }

  add_action('woocommerce_booking_add_to_cart',  'booking_add_to_cart');
}

The crucial part is this action add_action('woocommerce_booking_add_to_cart', 'booking_add_to_cart');

Which puts the add to cart button once in the right place - to use it in your own custom product make an action called woocommerce_YOURPRODUCTTYPE_add_to_cart


回答1:


Tried to test your code on a test server, but your booking product was not showing on backend.

Your issue could come from here: I think you are using with public function add_to_cart() an existing name function, and you have to rename-it differently.

Then I have revisited your code, based on this thread.

It's may be better to extend WC_Product_Simple class rather than WC_Product class, since your product uses simple product add-to-cart button template:
single-product/add-to-cart/simple.php.

With this custom code I don't face like you Multiple add to cart sections.

— @Update1 —
But you can still keep WC_Product extended instead of WC_Product_Simple. This way, as you requested in comment, your product type will be "simple_booking" as specified in 1st and 2nd functions. That does not mean that your product is simple (it's just a slug that you can change, changing at the same time in both places).

So I have modified the code again and tested: It works…

This is the code I use (and I have tested):

/**
 * Register the custom product type after init
 */
function register_simple_booking_product_type() {

    /**
     * This should be in its own separate file.
     */
    class WC_Product_Booking extends WC_Product{ // <= changed back to WC_product class

        public function __construct( $product ) {

            $this->product_type = 'simple_booking';
            $this->supports[]   = 'ajax_add_to_cart';

            parent::__construct( $product );

            // As we extend simple product class, you may not need this anymore.
            add_action('woocommerce_booking_add_to_cart', array($this, 'simple_booking_add_to_cart'),30);

        }

    }

}
add_action( 'init', 'register_simple_booking_product_type' );

// Registering the slug name (YOU can CHANGE IT)
function add_simple_booking_product( $types ){

    // Key should be exactly the same as in the class product_type parameter
    $types[ 'simple_booking' ] = __( 'Simple booking' );

    return $types;

}
add_filter( 'product_type_selector', 'add_simple_booking_product' );

/**
 * Show pricing fields for simple_booking product.
 */
function simple_booking_custom_js() {

    if ( 'product' != get_post_type() ) :
        return;
    endif;

    ?><script type='text/javascript'>
        jQuery( document ).ready( function() {
            jQuery( '.options_group.pricing' ).addClass( 'show_if_simple_booking' ).show();
        });
    </script><?php
}
add_action( 'admin_footer', 'simple_booking_custom_js' );

// Not sure you will need that (because of the change of the extended class)
function simple_booking_add_to_cart() {
    wc_get_template( 'single-product/add-to-cart/simple.php' );
}

Tested this code on function.php file located in active child theme (or theme).

Reference: Adding a custom WooCommerce product type


— @Update2 — The solution (find by the author of this question):

To puts the add-to-cart button once in the right place (to use it in your own custom product make an action called woocommerce_YOURPRODUCTTYPE_add_to_cart), with this code:

if (! function_exists( 'woocommerce_booking_add_to_cart' ) ) {

  /**
  * Output the simple product add to cart area.
  *
  * @subpackage Product
  */

  function booking_add_to_cart() {
    wc_get_template( 'single-product/add-to-cart/simple.php' );
  }

  add_action('woocommerce_booking_add_to_cart',  'booking_add_to_cart');
}


来源:https://stackoverflow.com/questions/38432736/woocommerce-custom-product-type-multiple-add-to-cart-sections-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!