Block some country to buy some manufacturers products

Started by neo2500, March 28, 2013, 11:47:45 AM

Previous topic - Next topic

neo2500

Hi everyone,

I want to develop a plugin to forbid Bulgarian people to buy products made by one manufacturer.
I want to display, on the checkout page, if the user have a Bulgarian address and have take wrong manufacturers products to redirect him to the checkout page.
I have done this in helper.php

<?php 
private function isBulgarianShip() {
                   
$virtuemart_country_id =
                   
VirtueMartCart::getCart()->BT['virtuemart_country_id'];
                      if(
shopFunctions::getCountryById($virtuemart_country_id)=="Bulgaria")
                      return 
true;
                   return 
false;
}
?>



<?php 
private function haveWrongProduct() {
                   
$db JFactory::getDBO();
                   
$query 'SELECT `virtuemart_manufacturer_id` FROM `#__virtuemart_manufacturers_en_gb` WHERE slug="wrong"';
                   
$db->setQuery($query);
                   
$wrongId =  $db->loadResult();
                   foreach (
VirtueMartCart::getCart()->products as $product) {
                           
$p = (array) $product;
                           if(
$p['virtuemart_manufacturer_id']==$wrongId)
                                   return 
true;
                   }
                   return 
false;
}
?>




But I didn't find how to develop a plugin for this. I didn't find the right event. I've try this

<?php

// no direct access
defined('_JEXEC') or die;

class 
plgblockCountry extends vmCustomPlugin
{
function __construct(& $subject$config) {
parent::__construct($subject$config);

$this->_tablepkey 'id';
$this->tableFields =
array_keys($this->getTableSQLFields());
$varsToPush = array(
'from'=>array('1977-01-01','date'),
'to'=>array('1977-01-01','date')
);

$this->setConfigParameterable('custom_params',$varsToPush);
}
public function plgVmOnViewCart($product$param,$productCustom,$row)
{
return "<p>Hello World!</p>";
}
}

?>



But it display nothing.

neo2500

Hi

I try to modify plugins/stockable/stockable.php . I added isBulgrianShip() and isWrongProduct() function and modify plgVmAddToCart function .


<?php
        
private function isBulgarianShip() {
$virtuemart_country_id VirtueMartCart::getCart()->BT['virtuemart_country_id'];
if(shopFunctions::getCountryById($virtuemart_country_id)=="Bulgaria")
return true;
return false;
}

private function isWrongProduct(&$product) {
$db JFactory::getDBO();
$query 'SELECT `virtuemart_manufacturer_id` FROM `#__virtuemart_manufacturers_en_gb` WHERE slug="wrong"';
$db->setQuery($query);
$wrongId =  $db->loadResult();
$p = (array) $product;
if($p['virtuemart_manufacturer_id']==$wrongId)
return true;
return false;
}


public function plgVmOnAddToCart(&$product){
$customPlugin JRequest::getVar('customPlugin',0);

if($this->isBulgarianShip() && $this->isWrongProduct($product) ) {
return false;
}

if ($customPlugin) {
$db JFactory::getDBO();
$query 'SELECT  C.* , field.*
FROM `#__virtuemart_customs` AS C
LEFT JOIN `#__virtuemart_product_customfields` AS field ON C.`virtuemart_custom_id` = field.`virtuemart_custom_id`
WHERE `virtuemart_product_id` =' 
$product->virtuemart_product_id.' and `custom_element`="'.$this->_name.'"';
$query .=' and is_cart_attribute = 1';
$db->setQuery($query);
$productCustomsPrice $db->loadObject();
if (!$productCustomsPrice) return null;
// if ( !in_array($this->_name,$customPlugin[$productCustomsPrice->virtuemart_custom_id]) ) return false;
$selected $customPlugin[$productCustomsPrice->virtuemart_customfield_id]['stockable']['child_id'];

if (!$child $this->plgVmCalculateCustomVariant($product$productCustomsPrice,$selected) ) return false;
if ($child->product_sku)
$product->product_sku $child->product_sku;
if ($child->product_name)
$product->product_name $child->product_name;
$product->product_in_stock $child->product_in_stock;
}

}
?>



That works on my local website but not on my online website ...