Skip to content

Commit 6a3828d

Browse files
committed
v3.0.6
Enhanced caching and order subtotal display.
1 parent 5e0e085 commit 6a3828d

File tree

10 files changed

+201
-42
lines changed

10 files changed

+201
-42
lines changed

app/code/community/PriceWaiter/NYPWidget/Block/Widget.php

+115-13
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,127 @@
33
class PriceWaiter_NYPWidget_Block_Widget extends Mage_Core_Block_Template
44
{
55
/**
6-
* @return PriceWaiter_NYPWidget_Model_Embed
6+
* @return array Bits of information used to make up the cache key.
77
*/
8-
public function getEmbed()
8+
public function getCacheKeyInfo()
9+
{
10+
$items = parent::getCacheKeyInfo();
11+
12+
if (!is_array($items)) {
13+
$items = array();
14+
}
15+
16+
$items[] = 'PriceWaiter';
17+
18+
19+
// 1. Cache per-customer
20+
$customer = $this->getCustomer();
21+
$items[] = $customer ? $customer->getId() : 0;
22+
23+
// 2. Cache per-customer group
24+
$items[] = $this->getCustomerGroupId();
25+
26+
// 3. Cache per-product
27+
$product = $this->getProduct();
28+
$items[] = $product ? $product->getId() : 0;
29+
30+
// 4. Cache per-category
31+
$category = $this->getCategory();
32+
$items[] = $category ? $category->getId() : 0;
33+
34+
return $items;
35+
}
36+
37+
/**
38+
* @return Integer This block's cache lifetime (in seconds).
39+
*/
40+
public function getCacheLifetime()
41+
{
42+
return 7200;
43+
}
44+
45+
public function getCacheTags()
46+
{
47+
$tags = parent::getCacheTags();
48+
if (!is_array($tags)) {
49+
$tags = array();
50+
}
51+
52+
$tags[] = Mage_Catalog_Model_Product::CACHE_TAG;
53+
$tags[] = Mage_Catalog_Model_Category::CACHE_TAG;
54+
$tags[] = PriceWaiter_NYPWidget_Helper_Data::CACHE_TAG;
55+
56+
return $tags;
57+
}
58+
59+
/**
60+
* @return Mage_Catalog_Model_Category
61+
*/
62+
public function getCategory()
963
{
10-
// Figure out where + who we are...
11-
$product = Mage::registry('current_product');
12-
$store = Mage::app()->getStore();
1364
$category = Mage::registry('current_category');
65+
return $category && $category->getId() ? $category : false;
66+
}
1467

15-
$session = Mage::getSingleton('customer/session');
68+
/**
69+
* @return Mage_Customer_Model_Customer|false
70+
*/
71+
public function getCustomer()
72+
{
73+
$session = $this->getCustomerSession();
1674
$customer = $session->getCustomer();
17-
$customerGroupId = $session->getCustomerGroupId();
1875

19-
// ..and wire up an appropriate embed.
76+
if ($customer && $customer->getId()) {
77+
return $customer;
78+
}
79+
80+
return false;
81+
}
82+
83+
/**
84+
* @return Number
85+
*/
86+
public function getCustomerGroupId()
87+
{
88+
$session = $this->getCustomerSession();
89+
return $session->getCustomerGroupId();
90+
}
91+
92+
/**
93+
* @return Mage_Customer_Model_Session
94+
*/
95+
public function getCustomerSession()
96+
{
97+
return Mage::getSingleton('customer/session');
98+
}
99+
100+
/**
101+
* @return Mage_Catalog_Model_Product|false
102+
*/
103+
public function getProduct()
104+
{
105+
$product = Mage::registry('current_product');
106+
return $product && $product->getId() ? $product : false;
107+
}
108+
109+
/**
110+
* @return Mage_Core_Model_Store
111+
*/
112+
public function getStore()
113+
{
114+
return Mage::app()->getStore();
115+
}
116+
117+
/**
118+
* @return PriceWaiter_NYPWidget_Model_Embed
119+
*/
120+
public function getEmbed()
121+
{
20122
return Mage::getModel('nypwidget/embed')
21-
->setProduct($product)
22-
->setStore($store)
23-
->setCategory($category)
24-
->setCustomer($customer->getId() ? $customer : false)
25-
->setCustomerGroupId($customerGroupId);
123+
->setProduct($this->getProduct())
124+
->setStore($this->getStore())
125+
->setCategory($this->getCategory())
126+
->setCustomer($this->getCustomer())
127+
->setCustomerGroupId($this->getCustomerGroupId());
26128
}
27129
}

app/code/community/PriceWaiter/NYPWidget/Helper/Data.php

+12
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,18 @@ class PriceWaiter_NYPWidget_Helper_Data extends Mage_Core_Helper_Abstract
1616
const XML_PATH_DISABLED_BY_CATEGORY = 'pricewaiter/categories/disable_by_category';
1717
const XML_PATH_SECRET = 'pricewaiter/configuration/api_secret';
1818

19+
const CACHE_TAG = 'pricewaiter_configuration';
20+
21+
/**
22+
* Clears any caches dependent on PriceWaiter config data.
23+
*/
24+
public function clearCache()
25+
{
26+
Mage::app()->cleanCache(array(
27+
self::CACHE_TAG,
28+
));
29+
}
30+
1931
/**
2032
* @return String URL of the PriceWaiter API.
2133
*/

app/code/community/PriceWaiter/NYPWidget/Model/Callback.php

+14-4
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,16 @@ class PriceWaiter_NYPWidget_Model_Callback
3131
*/
3232
protected $passwordCharacters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
3333

34+
private function getProductListPrice(Mage_Catalog_Model_Product $product)
35+
{
36+
$price = $product->getFinalPrice();
37+
if ($price) {
38+
return $price;
39+
}
40+
41+
return $product->getPrice();
42+
}
43+
3444
/**
3545
* @internal Adds items to the given order and calculates final total.
3646
* @param Mage_Sales_Model_Order $order
@@ -70,7 +80,7 @@ public function addItemsToOrder(
7080
// various amount fields present on the order item.
7181
$amounts = $this->calculateOrderItemAmounts(
7282
$request,
73-
$product->getPrice(),
83+
$this->getProductListPrice($product),
7484
array($store, 'roundPrice')
7585
);
7686
foreach($amounts as $key => $value) {
@@ -202,7 +212,7 @@ public function buildMagentoOrder(Array $request, Mage_Core_Model_Store $store,
202212
throw new PriceWaiter_NYPWidget_Exception_Product_NotFound();
203213
}
204214

205-
$amounts = $this->calculateOrderAmounts($request, $product->getPrice(), array($store, 'roundPrice'));
215+
$amounts = $this->calculateOrderAmounts($request, $this->getProductListPrice($product), array($store, 'roundPrice'));
206216

207217
foreach($amounts as $key => $value) {
208218
$order->setData($key, $value);
@@ -749,7 +759,7 @@ protected function applyCustomOptionPricesToProduct(
749759

750760
// Apply custom option price changes all in one go
751761
// (to avoid them interfering with each other)
752-
$product->setPrice($product->getPrice() + $amountToAdd);
762+
$product->setPrice($this->getProductListPrice($product) + $amountToAdd);
753763
}
754764

755765
/**
@@ -913,7 +923,7 @@ protected function resolveConfigurableProductForOrderWrite(
913923
}
914924

915925
$product->load($product->getId());
916-
$product->setPrice($product->getPrice() + $additionalCost);
926+
$product->setPrice($this->getProductListPrice($product) + $additionalCost);
917927

918928
return $product;
919929
}

app/code/community/PriceWaiter/NYPWidget/Model/Observer.php

+8
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ public function saveCategory(Varien_Event_Observer $observer)
3131
return true;
3232
}
3333

34+
/**
35+
* Called when the PriceWaiter configuration page is saved.
36+
*/
37+
public function handleConfigurationSave()
38+
{
39+
Mage::helper('nypwidget')->clearCache();
40+
}
41+
3442
/**
3543
* Called when the customer logs out.
3644
* @param Varien_Event_Observer $observer

app/code/community/PriceWaiter/NYPWidget/Model/Total/Quote.php

-18
Original file line numberDiff line numberDiff line change
@@ -467,24 +467,6 @@ protected function shouldCollectAddress(Mage_Sales_Model_Quote_Address $addr)
467467
}
468468
}
469469

470-
// (2) Don't allow if the sales rule applies to _more than shipping only_
471-
$ruleIds = $quote->getAppliedRuleIds();
472-
if (!empty($ruleIds)) {
473-
$helper = Mage::helper('nypwidget/rule');
474-
$ruleCollection = Mage::getModel('salesrule/rule')
475-
->getCollection()
476-
->addFieldToFilter('rule_id', array('in' => $ruleIds));
477-
478-
foreach ($ruleCollection as $rule) {
479-
if (!$helper->ruleAppliesToShippingOnly($rule)) {
480-
Mage::getSingleton('core/session')->addNotice(
481-
'Your offer discount could not be applied because of an existing promotion.'
482-
);
483-
return false;
484-
}
485-
}
486-
}
487-
488470
return true;
489471
}
490472

app/code/community/PriceWaiter/NYPWidget/etc/config.xml

+11-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
<modules>
55
<PriceWaiter_NYPWidget>
6-
<version>3.0.5</version>
6+
<version>3.0.6</version>
77
</PriceWaiter_NYPWidget>
88
</modules>
99

@@ -80,6 +80,16 @@
8080
</adminhtml_catalog_category_tabs_pricewaiter>
8181
</observers>
8282
</adminhtml_catalog_category_tabs>
83+
<admin_system_config_changed_section_pricewaiter>
84+
<observers>
85+
<admin_system_config_changed_section_pricewaiter>
86+
<type>model</type>
87+
<class>nypwidget/observer</class>
88+
<method>handleConfigurationSave</method>
89+
<args></args>
90+
</admin_system_config_changed_section_pricewaiter>
91+
</observers>
92+
</admin_system_config_changed_section_pricewaiter>
8393
<catalog_category_prepare_save>
8494
<observers>
8595
<catalog_category_prepare_save_pricewaiter>

docker/phpcs/Dockerfile

+2-3
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ RUN git clone https://github.com/magento/marketplace-eqp.git && \
2222
composer install && \
2323
cd ..
2424

25-
ENV PHPCS_CMD="php marketplace-eqp/vendor/squizlabs/php_codesniffer/scripts/phpcs"
26-
ENV PHPCBF_CMD="php marketplace-eqp/vendor/squizlabs/php_codesniffer/scripts/phpcbf"
25+
ENV PHPCS_CMD="php marketplace-eqp/vendor/bin/phpcs"
26+
ENV PHPCBF_CMD="php marketplace-eqp/vendor/bin/phpcbf"
2727

2828
# Set installed_paths, per README
2929
RUN $PHPCS_CMD --config-set installed_paths /usr/src/ext/marketplace-eqp
@@ -35,4 +35,3 @@ COPY js src/js
3535
COPY skin src/skin
3636

3737
CMD [ "run-phpcs" ]
38-

package.template.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
<?xml version="1.0"?>
22
<package>
33
<name>nypwidget</name>
4-
<version>3.0.5</version>
4+
<version>3.0.6</version>
55
<stability>stable</stability>
66
<license uri="http://www.apache.org/licenses/LICENSE-2.0.html">Apache License, Version 2.0</license>
77
<channel>community</channel>
88
<extends/>
99
<summary>PriceWaiter lets buyers make offers on your products that you can easily accept, counter offer or reject.</summary>
1010
<description>Sell more, immediately, with PriceWaiter. Convert comparison shoppers you might have lost, increase sales and conversions, and stop "minimum advertised price" from preventing sales before they even start.&#xD;&#xD; PriceWaiter lets customers make offers on products you sell-- offers you can accept, reject or counter. The widget embeds a simple Name Your Price button on any product page or category that you choose. Simply install the extension and you'll have full control over PriceWaiter in your Magento admin control panel.</description>
11-
<notes>Prevent discount warnings from displaying when no deals are present.</notes>
11+
<notes>Enhanced caching and order subtotal display.</notes>
1212
<authors>
1313
<author>
1414
<name>PriceWaiter</name>

tests/integration/OrderCallback/CustomOptionsTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function testOrderItemPriceSet(Array $args)
3939
$this->assertNotEmpty($item, 'order has an item');
4040

4141
$this->assertEquals(
42-
'154.00',
42+
'75.00',
4343
$item->getPrice()
4444
);
4545
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
require_once(__DIR__ . '/Base.php');
4+
5+
/**
6+
* Tests around PriceWaiter order callback.
7+
*/
8+
class Integration_OrderCallback_SpecialPrice
9+
extends Integration_OrderCallback_Base
10+
{
11+
public $product = array(
12+
'type' => 'simple',
13+
'sku' => 'hdb008',
14+
'id' => '384',
15+
'name' => 'Park Row Throw',
16+
);
17+
18+
public function testRespectsSpecialPrice()
19+
{
20+
list($request, $order, $callback) = $this->doOrderCallback();
21+
22+
$item = $order->getItemsCollection()->getFirstItem();
23+
$product = $item->getProduct();
24+
25+
// getSource()->getDiscountAmount()
26+
// $source->getDiscountDescription()
27+
28+
$this->assertEquals(-40.02, $order->getBaseDiscountAmount());
29+
$this->assertEquals(-40.02, $order->getDiscountAmount());
30+
$this->assertEquals(224.48, $order->getBaseGrandTotal());
31+
$this->assertEquals(240.00, $order->getBaseSubtotal());
32+
$this->assertEquals(240.00, $order->getSubtotal());
33+
$this->assertEquals(240.00, $product->getPrice());
34+
$this->assertEquals(120.00, $product->getSpecialPrice());
35+
}
36+
}

0 commit comments

Comments
 (0)