• Resolved sugud0r

    (@sugud0r)


    Hi. I’m currently trying to create a custom product type that extends WC_Product_Variable, but my problem are in cart: when cart render the items, it does not show correct item price of the variation, instead it show the ‘base’ price of the variable product.

    This is the current code of my custom product type.

    
    <?php
    
    if ( ! defined( 'ABSPATH' ) ) {
        return;
    }
    
    include_once INC_DIR . 'woocommerce/Sams_Salsa_Common_Slugs.php';
    
    /**
     * Class representation of Salsa Pack product type.
     *
     * @author Moisés González <mgonzalez@solomedia.co>
     */
    class WC_Salsa_Pack_Product_Type extends WC_Product_Variable implements Samsa_Salsa_Common_Slugs {
        /**
         * Product type
         *
         * @var string
         */
        protected $product_type = self::SALSA_PACK_SLUG;
    
        public function __construct( $product ) {
            parent::__construct( $product );
        }
    
        /**
         * Override get_type WC_Product method.
         *
         * @return string
         */
        public function get_type() {
            return $this->product_type;
        }
    
        /**
         * Override add_to_cart_url WC_Product method to
         * return the product permalink.
         *
         * @return string
         */
        public function add_to_cart_url() {
            $url = get_permalink( $this->get_id() );
    
            return apply_filters( 'woocommerce_product_add_to_cart_url', $url, $this );
        }
    
        /**
         * Override add_to_cart_text WC_Product method.
         *
         * @return string
         */
        public function add_to_cart_text() {
            $text = __( 'Choice your Salsas', 'sams-famous-salsa' );
    
            return apply_filters( 'woocommerce_product_add_to_cart_text', $text, $this );
        }
    }
    

    Also, I figured out that normals variable product instance WC_Product_Variation in cart, but variations of my product type still instance WC_Salsa_Pack_Product_Type. The is a way to change this behaviour?

    Screenshot of cart

    EDIT: Is a point bweteen Cart and Checkout. Seems that variation_id is not saved correctly and don’t get stored correctly in Cart session.

    • This topic was modified 3 years, 7 months ago by sugud0r. Reason: Miss formatting
    • This topic was modified 3 years, 7 months ago by sugud0r.
    • This topic was modified 3 years, 7 months ago by sugud0r.
    • This topic was modified 3 years, 7 months ago by sugud0r.
    • This topic was modified 3 years, 7 months ago by sugud0r.
    • This topic was modified 3 years, 7 months ago by sugud0r. Reason: Add hint
Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Contributor Niels Lange

    (@nielslange)

    Hello @sugud0r,

    This is a fairly complex development topic. I’m going to leave it open for a bit to see if anyone is able to chime in to help you out.

    I can also recommend the following places for more development-oriented questions:

    1. WooCommerce Slack Community: https://woocommerce.com/community-slack/
    2. WooCommerce FB group: https://www.facebook.com/groups/advanced.woocommerce/
    Thread Starter sugud0r

    (@sugud0r)

    My issues was solved. For any those who have the same problem: When you create a custom producty type that inherits WC_Product_Variable you need to add woocommerce_add_to_cart_handler filter and return variable to tell WooCommerce the correct handler for your product type when the product is added to cart.

    Sample:

    
    /**
     * Set variable to handler add to cart
     *
     * @param string $handler
     * @param WC_Product $product
     * @return string
     */
    public static function set_correct_add_to_cart_handler( $handler, $product ) {
        if ( $product->is_type( 'your type' ) ) {
            $handler = 'the hanlder'; # variable, simple or grouped
        }
    
        return $handler;
    }
    
    add_filter( 'woocommerce_add_to_cart_handler', 'set_correct_add_to_cart_handler', 10, 2 );
    

    This also apply with simple or grouped products types (remember that you need to inherit the correct WC_Product_* class to create your custom product type).

    This behaviour is defined in woocommerce/includes/class-wc-form-handler.php line 769 for WooCommerce 4.4.1

    Also, you can define your own handler with the action 'woocommerce_add_to_cart_handler_' . $your_product_type, but I guess that you need to really know what you are doing.

    Hi @sugud0r ,

    I create a custom product type that inherits WC_Product_Variable, but my variabtions is not showing in front-end, am just getting message “This product is currently out of stock and unavailable”

    Hi @sugud0r ,

    I am about to create a custom product type like Variable Product type, I couldn’t find any article or help document. is it possible for you to share your code to refer to or any online help link?

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Trying to create a custom variable product type, but get wrong price in cart’ is closed to new replies.