0

On our multisite network, I have a plugin with

if( is_main_site(get_current_blog_id()) ) WC()->cart = new WC_Custom_Cart;


class WC_Custom_Cart extends WC_Cart
{
  public function __construct() {

  }

  public function init() {

  }

  /**
   * Calculate totals for the items in the cart.
   *
   * @uses WC_Cart_Totals
   */
  public function calculate_totals() {
    $this->reset_totals();
  }

  /**
     * Add a product to the cart.
     *
     * @throws Exception Plugins can throw an exception to prevent adding to cart.
     * @param int   $product_id contains the id of the product to add to the cart.
     * @param int   $quantity contains the quantity of the item to add.
     * @param int   $variation_id ID of the variation being added to the cart.
     * @param array $variation attribute values.
     * @param array $cart_item_data extra cart item data we want to pass into the item.
     * @return string|bool $cart_item_key
     */
    public function add_to_cart( $product_id = 0, $quantity = 1, $variation_id = 0, $variation = array(), $cart_item_data = array() ) {}
}

This doesn't seem to be doing anything, as when I add a product to the cart, it should reset the totals and then not set them, but they're still showing as normal.

Ultimately what I want is for on the main site, products including variable products can be added to the cart in entirety (no variation selection required), as they are only being selected for importing into subsites, not purchase. Pricing should show the custom fields 'Cost' and 'MSRP' instead of price/sale price, but charge nothing.

UPDATE: I changed the 1st line to this:

class WooCustom
{
  public function __construct()
  {
    if( is_main_site(get_current_blog_id()) ) {
      require_once('classes/wc-custom-cart.php');
      // setup woocommerce to use your cart class
      add_action( 'woocommerce_init', 'wpwc7321_woocommerce_init' );
      function wpwc7321_woocommerce_init() {
          if ( !is_admin() || defined( 'DOING_AJAX' ) ) {
              WC()->cart = new WC_Custom_Cart();
              echo WC()->cart->get_total();
          }
      }
    }
  }
}
$GLOBALS['woocustom'] = new WooCustom();

I copied the WC_Cart class to mine, changed the get_total() method to return 100, and indeed that is the output above. However when I add a product to the cart via AJAX in the archives, then view the cart, it's always empty. I changed the echo line above to WC->cart->what() and emptied my custom extension class except for the what method, which returns $this->get_total() and it does return the standard value, $0.00.

How do I effectively substitute/override some of these methods in my custom class?!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.