开发者

PHP Magento/Google import script

开发者 https://www.devze.com 2023-04-10 00:35 出处:网络
I have the following script taken from another Stack Overflow answer. <?php define(\'SAVE_FEED_LOCATION\',\'google_base_feed.txt\');

I have the following script taken from another Stack Overflow answer.

<?php
define('SAVE_FEED_LOCATION','google_base_feed.txt');
set_time_limit(1800);
require_once '../app/Mage.php';
Mage::app('default');
try{
    $handle = fopen(SAVE_FEED_LOCATION, 'w');

    $heading = array('id','mpn', 'upc','title','description','link','image_link','price','brand','product_type','condition', 'google_product_category', 'manufacturer', 'availability');
    $feed_line=implode("\t", $heading)."\r\n";
    fwrite($handle, $feed_line);

    $products = Mage::getModel('catalog/product')->getCollection();
    $products->addAttributeToFilter('status', 1);
    $products->addAttributeToFilter('visibility', 4);
    $products->addAttributeToSelect('*');
    $prodIds=$products->getAllIds();

    $product = Mage::getModel('catalog/product');

    $counter_test = 0;

    foreach($prodIds as $productId) {

        if (++$counter_test < 30000){

            $product->load($productId);

            $product_data = array();
            $product_data['sku'] = $product->getSku();
            $product_data['mpn'] = $product->getData('upc');
            $product_data['upc'] = $product->getData('upc');

            $title_temp = $product->getName();
            if (strlen($title_temp) > 70){
                $title_temp = str_replace("Supply", "", $title_temp);
                $title_temp = str_replace("  ", " ", $title_temp);
            }
            $product_data['title'] = $title_temp;


            $product_data['description'] = substr(iconv("UTF-8","UTF-8//IGNORE",$product->getDescription()), 0, 900);
            $product_data['Deeplink'] = "http://www.myshop.co.uk/store/".$product->getUrlPath(); 
            $product_data['image_link'] = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA).'catalog/product'.$product->getImage();

            $price_temp = round($product->getPrice(),2);
开发者_StackOverflow            $product_data['price'] = round($product->getPrice(),2) + 5;

            $product_data['brand'] = $product->getData('brand');

            $product_data['product_type'] = 'Pet Products and Accessories';
            $product_data['condition'] = "new";
            $product_data['category'] = $product_data['brand'];
            $product_data['manufacturer'] = $product_data['brand'];

            $product_data['availability'] = "in stock";

            foreach($product_data as $k=>$val){
                $bad=array('"',"\r\n","\n","\r","\t");
                $good=array(""," "," "," ","");
                $product_data[$k] = '"'.str_replace($bad,$good,$val).'"';
            }

            echo $counter_test  . " ";

            $feed_line = implode("\t", $product_data)."\r\n";
            fwrite($handle, $feed_line);
            fflush($handle);

        }

    }

    fclose($handle);
}
catch(Exception $e){
    die($e->getMessage());
}

If you look at the section that looks up the product price:

$product_data['price'] = round($product->getPrice(),2) + 5;

I don't understand what is happening in that line. For some reason the script is altering our price.

The cost price of the example is £52.80, with a retail price of £56.54. The script is resulting in a price of £61.54.

The script should be taking the price (£56.54) and adding VAT (20%). So once the script has been run, in the results text file the price should be £67.85.

A) What is the price line actually doing? B) How do I change it to add 20% to the price?


The price line is round the $product->getPrice() value to two decimal places, adding 5 and then assigning the value to $product_data['price'].

To increase a number by a percentage of itself, say 20%, you multiply the number by itself plus the percentage you want (as a fraction of 1):

$with_vat_number = $no_vat_number * 1.20;

So to get the final rounded number, use round:

$rounded_with_vat_number = round(($no_vat_number * 1.20), 2);


Another way to have your prices in your Google Feed show as inclusive of vat, is to replace below:

$price_temp = round($product->getPrice(),2);
$product_data['price'] = round($product->getPrice(),2) + 5;

With the Below Code:

//VAT Stuff
$vat = 20; //percent
$pvat = $vat / 100;
$product_data['price']= round( ($product->getPrice() + $product->getPrice() * $pvat),2);

This way when VAT amount changes, then instead of having to work out fractions etc, just change the "$vat = 20; to whatever the new Vat Percentage is. So when it decreases back to 17.5 then you change the code to "$vat = 17.5;" (without quotation marks off course!)

0

精彩评论

暂无评论...
验证码 换一张
取 消