如何在特定位置结账时限制特定产品
How to restrict particular products at checkout for particuar location
我只想在德里销售一些产品,其余产品将在印度全境销售。那么如何在除德里 ncr 以外的其他位置限制结帐页面上的产品,例如,我只想在德里销售蛋糕,所以如果有人继续结帐蛋糕产品并且客户将其他位置放在除德里 ncr 以外的其他位置,那么我如何限制该客户下订单.. 请帮忙
您可以使用事件观察器来完成此任务。这样您就可以在客户尝试浏览结帐时检查他们的购物车。
一个可能对使用有帮助的事件是checkout_controller_onepage_save_shipping_method。
您可以收听许多事件,但这对于这个问题来说似乎非常安全。
该事件发生在客户保存其送货方式之后。现在您知道他们希望将其运往何处。有了这些知识,您就可以检查客户的购物车,验证是否可以继续或停止进度,并将他们重定向到自定义页面。另一种选择是 CMS 页面,以显示他们无法继续的原因。
最后一个选项是在此时抛出异常,并让错误消息描述他们无法继续的原因。
我会将其设计得更复杂一些,但为了让您了解事件观察器,我将尽量保持简单。
以下是完成此任务所需的步骤:
- 在您担心限制运输的产品的属性集上创建一个属性,我们可以在结账时检查购物车时使用该属性。可能是这样的:delhi_only。我会让它成为一个 yes/no 产品属性并在前端可见
在我们的模块中创建事件观察器。我喜欢在预期模块中保持相关的东西,所以我们将创建一个名为 Gallup_Checkout 的模块。在 app/etc/modules 中创建模块声明,它应该如下所示
<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<Gallup_Checkout>
<active>true</active>
<codePool>local</codePool>
</Gallup_Checkout>
</modules>
</config>
在 app/code/local 我们将创建一个名为 Gallup
的文件夹
在 app/code/local/Gallup 中,我们需要一个 etc 文件夹 config.xml inside
<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<Gallup_Checkout>
<version>1.0.0</version>
</Gallup_Checkout>
</modules>
<global>
<events>
<checkout_controller_onepage_save_shipping_method>
<observers>
<gallup_checkout>
<type>singleton</type>
<class>gallup_checkout/observer</class>
<method>checkoutControllerOnepageSaveShippingMethod</method>
</gallup_checkout>
</observers>
</checkout_controller_onepage_save_shipping_method>
</events>
<helpers>
<gallup_checkout>
<class>Gallup_Checkout_Helper</class>
</gallup_checkout>
</helpers>
</global>
</config>
现在在 app/code/local/Gallup/Checkout/Helper/Data.php 中创建空白助手
class Gallup_Checkout_Helper_Data extends Mage_Checkout_Helper_Data{
}
最后是你的观察者classapp/code/local/Gallup/Checkout/Model/Observer.php
class Gallup_Checkout_Model_Observer{
/**
* @param Varien_Event_Observer $observer
*/
public function checkoutControllerOnepageSaveShippingMethod( Varien_Event_Observer $observer ){
// use a private function so we can do other things in this same event observer if needed in the future
$this->_checkoutControllerOnepageSaveShippingMethod( $observer );
}
private function _checkoutControllerOnepageSaveShippingMethod( $observer ){
$quote = $observer->getQuote();
foreach ($quote->getAllItems() as $_item){
$_dehliAddress = $this->_checkDehliShippingAddress($quote);
$_checkDehliOnly = $this->_checkDehliOnly($_item->getData('product_id'));
/**
* Check if this product is dehli only AND the address failed, if so alert the customer
*/
if ( $_checkDehliOnly && !$_dehliAddress){
$error_message = 'Sorry, we are unable to continue, you have items in your cart that need to be delivered within Dehli';
Mage::getSingleton('core/session')->addError($error_message);
Mage::throwException($error_message);
}
}
}
/**
* @param $product
* @return int
*/
private function _checkDehliOnly($product_id){
$product = Mage::getModel('catalog/product')->load($product_id);
// This is saved as a string 0 or 1 so casting it to an int will give us the boolean we are expecting
$delhi_only = $product->getData('delhi_only');
return (int)$delhi_only;
}
/**
* Do you logic that determines if we are prohibiting shipping
* @param $quote
*/
private function _checkDehliShippingAddress( $quote ){
// Get the shipping address from the quote
$shipping_address = $quote->getShippingAddress();
/**
* I would suggest better validation than this but hopefully this will give you an idea on how to proceed.
*/
$city = trim(strtolower($shipping_address->getData('city')));
// check if the city is dehli and if so return true
if(($city == 'dehli')){
// return true so we know its OK to proceed
return true;
}else{
// This is not a match, return false so we know that its not allowed
return false;
}
}
以下是一些可能有用的屏幕截图
设置自定义产品属性
现在是自定义产品属性的标签
不要忘记将此新产品属性分配给属性集。
现在编辑属于该属性集的产品和我们的新产品属性
现在,我们可以测试前端体验了
在结帐期间,一旦该地址被保存,我们就拥有了最终验证所需的所有信息,可以继续或停止它们并将它们 return 放入购物车,但出现错误:
我们在购物车中发现一件商品停止结帐将它们带回购物车并显示错误消息
我只想在德里销售一些产品,其余产品将在印度全境销售。那么如何在除德里 ncr 以外的其他位置限制结帐页面上的产品,例如,我只想在德里销售蛋糕,所以如果有人继续结帐蛋糕产品并且客户将其他位置放在除德里 ncr 以外的其他位置,那么我如何限制该客户下订单.. 请帮忙
您可以使用事件观察器来完成此任务。这样您就可以在客户尝试浏览结帐时检查他们的购物车。
一个可能对使用有帮助的事件是checkout_controller_onepage_save_shipping_method。
您可以收听许多事件,但这对于这个问题来说似乎非常安全。
该事件发生在客户保存其送货方式之后。现在您知道他们希望将其运往何处。有了这些知识,您就可以检查客户的购物车,验证是否可以继续或停止进度,并将他们重定向到自定义页面。另一种选择是 CMS 页面,以显示他们无法继续的原因。
最后一个选项是在此时抛出异常,并让错误消息描述他们无法继续的原因。
我会将其设计得更复杂一些,但为了让您了解事件观察器,我将尽量保持简单。
以下是完成此任务所需的步骤:
- 在您担心限制运输的产品的属性集上创建一个属性,我们可以在结账时检查购物车时使用该属性。可能是这样的:delhi_only。我会让它成为一个 yes/no 产品属性并在前端可见
在我们的模块中创建事件观察器。我喜欢在预期模块中保持相关的东西,所以我们将创建一个名为 Gallup_Checkout 的模块。在 app/etc/modules 中创建模块声明,它应该如下所示
<?xml version="1.0" encoding="UTF-8"?> <config> <modules> <Gallup_Checkout> <active>true</active> <codePool>local</codePool> </Gallup_Checkout> </modules> </config>
在 app/code/local 我们将创建一个名为 Gallup
的文件夹
在 app/code/local/Gallup 中,我们需要一个 etc 文件夹 config.xml inside
<?xml version="1.0" encoding="UTF-8"?> <config> <modules> <Gallup_Checkout> <version>1.0.0</version> </Gallup_Checkout> </modules> <global> <events> <checkout_controller_onepage_save_shipping_method> <observers> <gallup_checkout> <type>singleton</type> <class>gallup_checkout/observer</class> <method>checkoutControllerOnepageSaveShippingMethod</method> </gallup_checkout> </observers> </checkout_controller_onepage_save_shipping_method> </events> <helpers> <gallup_checkout> <class>Gallup_Checkout_Helper</class> </gallup_checkout> </helpers> </global> </config>
现在在 app/code/local/Gallup/Checkout/Helper/Data.php 中创建空白助手
class Gallup_Checkout_Helper_Data extends Mage_Checkout_Helper_Data{ }
最后是你的观察者classapp/code/local/Gallup/Checkout/Model/Observer.php
class Gallup_Checkout_Model_Observer{ /** * @param Varien_Event_Observer $observer */ public function checkoutControllerOnepageSaveShippingMethod( Varien_Event_Observer $observer ){ // use a private function so we can do other things in this same event observer if needed in the future $this->_checkoutControllerOnepageSaveShippingMethod( $observer ); }
private function _checkoutControllerOnepageSaveShippingMethod( $observer ){ $quote = $observer->getQuote(); foreach ($quote->getAllItems() as $_item){ $_dehliAddress = $this->_checkDehliShippingAddress($quote); $_checkDehliOnly = $this->_checkDehliOnly($_item->getData('product_id')); /** * Check if this product is dehli only AND the address failed, if so alert the customer */ if ( $_checkDehliOnly && !$_dehliAddress){ $error_message = 'Sorry, we are unable to continue, you have items in your cart that need to be delivered within Dehli'; Mage::getSingleton('core/session')->addError($error_message); Mage::throwException($error_message); } } } /** * @param $product * @return int */ private function _checkDehliOnly($product_id){ $product = Mage::getModel('catalog/product')->load($product_id); // This is saved as a string 0 or 1 so casting it to an int will give us the boolean we are expecting $delhi_only = $product->getData('delhi_only'); return (int)$delhi_only; } /** * Do you logic that determines if we are prohibiting shipping * @param $quote */ private function _checkDehliShippingAddress( $quote ){ // Get the shipping address from the quote $shipping_address = $quote->getShippingAddress(); /** * I would suggest better validation than this but hopefully this will give you an idea on how to proceed. */ $city = trim(strtolower($shipping_address->getData('city'))); // check if the city is dehli and if so return true if(($city == 'dehli')){ // return true so we know its OK to proceed return true; }else{ // This is not a match, return false so we know that its not allowed return false; } }
以下是一些可能有用的屏幕截图 设置自定义产品属性
现在是自定义产品属性的标签
不要忘记将此新产品属性分配给属性集。
现在编辑属于该属性集的产品和我们的新产品属性
现在,我们可以测试前端体验了
在结帐期间,一旦该地址被保存,我们就拥有了最终验证所需的所有信息,可以继续或停止它们并将它们 return 放入购物车,但出现错误:
我们在购物车中发现一件商品停止结帐将它们带回购物车并显示错误消息