Apply YITH Role Based Prices to Configured Products

YITH WooCommerce Role Based Prices replaces a cart item’s price with the role price of the product alone, so the price of the options chosen in the Product Configurator form is dropped from the cart.

This snippet charges the product’s role-based price plus the form’s options.

Instructions

Code

Filename: gspc-yith-role-based-prices.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?php
/**
 * Gravity Shop // Product Configurator // Apply YITH Role Based Prices to Configured Products
 * https://gravitywiz.com/documentation/gravity-shop-product-configurator/
 *
 * YITH WooCommerce Role Based Prices replaces a cart item's price with the role price of the product alone,
 * so the price of the options chosen in the Product Configurator form is dropped from the cart.
 *
 * This snippet charges the product's role-based price plus the form's options.
 *
 * Instructions:
 *
 *   1. Install per https://gravitywiz.com/documentation/how-do-i-install-a-snippet/
 */
class GSPC_YITH_Role_Based_Prices {

	private $priced_products;

	public function __construct() {
		$this->priced_products = new SplObjectStorage();

		// Run after GSPC adds the form's options to the cart item's price.
		add_filter( 'woocommerce_add_cart_item', array( $this, 'apply_role_based_price' ), 20 );
		add_filter( 'woocommerce_get_cart_item_from_session', array( $this, 'apply_role_based_price' ), 30 );

		// Stop YITH from replacing the price set here.
		add_filter( 'yith_wcrbp_return_original_price', array( $this, 'keep_cart_item_price' ), 10, 3 );
	}

	public function apply_role_based_price( $cart_item ) {
		if ( empty( $cart_item['gspc_gf_entry_ids'] ) || ! function_exists( 'YITH_Role_Based_Prices_Product' ) ) {
			return $cart_item;
		}

		$product        = $cart_item['data'];
		$stored_product = wc_get_product( $product->get_id() );

		$base_price    = (float) $stored_product->get_price( 'edit' );
		$options_price = (float) $product->get_price( 'edit' ) - $base_price;
		$role_price    = (float) YITH_Role_Based_Prices_Product()->get_price( $base_price, $stored_product );

		$product->set_price( $role_price + $options_price );
		$this->priced_products->attach( $product );

		return $cart_item;
	}

	public function keep_cart_item_price( $return_original, $price, $product ) {
		if ( $product instanceof WC_Product && $this->priced_products->contains( $product ) ) {
			return true;
		}

		return $return_original;
	}
}

new GSPC_YITH_Role_Based_Prices();

Leave a Reply

Your email address will not be published. Required fields are marked *

  • Trouble installing this snippet? See our troubleshooting tips.
  • Need to include code? Create a gist and link to it in your comment.
  • Reporting a bug? Provide a URL where this issue can be recreated.

By commenting, I understand that I may receive emails related to Gravity Wiz and can unsubscribe at any time.