Example #1
0
 /**
  * Set current attribute to entry (for specified product)
  *
  * @param Mage_Catalog_Model_Product $product
  * @param Google_Service_ShoppingContent_Product $shoppingProduct
  * @return Google_Service_ShoppingContent_Product
  */
 public function convertAttribute($product, $shoppingProduct)
 {
     $url = $product->getProductUrl(false);
     if ($url) {
         $config = Mage::getSingleton('googleshoppingapi/config');
         if (!Mage::getStoreConfigFlag('web/url/use_store') && $config->getAddStoreCodeToUrl()) {
             Mage::log($config->getAddStoreCodeToUrl() ? "true" : "false");
             $urlInfo = parse_url($url);
             $store = $product->getStore()->getCode();
             if (isset($urlInfo['query']) && $urlInfo['query'] != '') {
                 $url .= '&___store=' . $store;
             } else {
                 $url .= '?___store=' . $store;
             }
         }
         if ($config->getAddUtmSrcGshopping($product->getStoreId())) {
             $url .= '&utm_source=GoogleShopping';
         }
         if ($customUrlParameters = $config->getCustomUrlParameters($product->getStoreId())) {
             $url .= $customUrlParameters;
         }
         $shoppingProduct->setLink($url);
     }
     return $shoppingProduct;
 }
 /**
  * Set current attribute to entry (for specified product)
  *
  * @param Mage_Catalog_Model_Product $product
  * @param Google_Service_ShoppingContent_Product $shoppingProduct
  * @return Google_Service_ShoppingContent_Product
  */
 public function convertAttribute($product, $shoppingProduct)
 {
     $availableUnits = array('mg', 'g', 'kg', 'ml', 'cl', 'l', 'cbm', 'cm', 'm', 'sqm');
     $basePriceAmount = $product->getBasePriceAmount();
     $basePriceUnit = strtolower($product->getBasePriceUnit());
     $unitPricingMeasure = $basePriceAmount . ' ' . $basePriceUnit;
     $basePriceReferenceAmount = $product->getBasePriceBaseAmount();
     $basePriceReferenceUnit = strtolower($product->getBasePriceBaseUnit());
     $unitPricingBaseMeasure = $basePriceReferenceAmount . ' ' . $basePriceReferenceUnit;
     // skip attribute if unit not available
     if (!in_array($basePriceUnit, $availableUnits) || !in_array($basePriceReferenceUnit, $availableUnits)) {
         return $shoppingProduct;
     }
     if (!empty($basePriceAmount) && !empty($basePriceReferenceAmount)) {
         $unitPricingMeasure = new Google_Service_ShoppingContent_ProductUnitPricingMeasure();
         $unitPricingMeasure->setUnit($basePriceUnit);
         $unitPricingMeasure->setValue($basePriceAmount);
         $unitPricingBaseMeasure = new Google_Service_ShoppingContent_ProductUnitPricingBaseMeasure();
         $unitPricingBaseMeasure->setUnit($basePriceReferenceUnit);
         $unitPricingBaseMeasure->setValue($basePriceReferenceAmount);
         $shoppingProduct->setUnitPricingMeasure($unitPricingMeasure);
         $shoppingProduct->setUnitPricingBaseMeasure($unitPricingBaseMeasure);
     }
     return $shoppingProduct;
 }
 /**
  * Set current attribute to entry (for specified product)
  *
  * @param Mage_Catalog_Model_Product $product
  * @param Google_Service_ShoppingContent_Product $shoppingProduct
  * @return Google_Service_ShoppingContent_Product
  */
 public function convertAttribute($product, $shoppingProduct)
 {
     $effectiveDateFrom = $this->getGroupAttributeSalePriceEffectiveDateFrom();
     $fromValue = $effectiveDateFrom->getProductAttributeValue($product);
     $effectiveDateTo = $this->getGroupAttributeSalePriceEffectiveDateTo();
     $toValue = $effectiveDateTo->getProductAttributeValue($product);
     $from = $to = null;
     if (!empty($fromValue) && Zend_Date::isDate($fromValue, Zend_Date::ATOM)) {
         $from = new Zend_Date($fromValue, Zend_Date::ATOM);
     }
     if (!empty($toValue) && Zend_Date::isDate($toValue, Zend_Date::ATOM)) {
         $to = new Zend_Date($toValue, Zend_Date::ATOM);
     }
     $dateString = null;
     // if we have from an to dates, and if these dates are correct
     if (!is_null($from) && !is_null($to) && $from->isEarlier($to)) {
         $dateString = $from->toString(Zend_Date::ATOM) . '/' . $to->toString(Zend_Date::ATOM);
     }
     // if we have only "from" date, send "from" day
     if (!is_null($from) && is_null($to)) {
         $dateString = $from->toString('YYYY-MM-dd');
     }
     // if we have only "to" date, use "now" date for "from"
     if (is_null($from) && !is_null($to)) {
         $from = new Zend_Date();
         // if "now" date is earlier than "to" date
         if ($from->isEarlier($to)) {
             $dateString = $from->toString(Zend_Date::ATOM) . '/' . $to->toString(Zend_Date::ATOM);
         }
     }
     if (!is_null($dateString)) {
         $shoppingProduct->setSalePriceEffectiveDate($dateString);
     }
     return $shoppingProduct;
 }
 /**
  * Set current attribute to entry (for specified product)
  *
  * @param Mage_Catalog_Model_Product $product
  * @param Google_Service_ShoppingContent_Product $shoppingProduct
  * @return Google_Service_ShoppingContent_Product
  */
 public function convertAttribute($product, $shoppingProduct)
 {
     $config = Mage::getSingleton('googleshoppingapi/config');
     $targetCountry = $config->getTargetCountry($product->getStoreId());
     $value = $config->getCountryInfo($targetCountry, 'language', $product->getStoreId());
     $shoppingProduct->setContentLanguage($value);
 }
Example #5
0
 /**
  * Set current attribute to entry (for specified product)
  *
  * @param Mage_Catalog_Model_Product $product
  * @param Google_Service_ShoppingContent_Product $shoppingProduct
  * @return Google_Service_ShoppingContent_Product
  */
 public function convertAttribute($product, $shoppingProduct)
 {
     if (Mage::helper('tax')->getConfig()->priceIncludesTax()) {
         return $shoppingProduct;
     }
     $calc = Mage::helper('tax')->getCalculator();
     $customerTaxClass = $calc->getDefaultCustomerTaxClass($product->getStoreId());
     $rates = $calc->getRatesByCustomerAndProductTaxClasses($customerTaxClass, $product->getTaxClassId());
     $targetCountry = Mage::getSingleton('googleshoppingapi/config')->getTargetCountry($product->getStoreId());
     $ratesTotal = 0;
     $taxes = array();
     foreach ($rates as $rate) {
         if ($targetCountry == $rate['country']) {
             $regions = $this->_parseRegions($rate['state'], $rate['postcode']);
             $ratesTotal += count($regions);
             if ($ratesTotal > self::RATES_MAX) {
                 Mage::throwException(Mage::helper('googleshoppingapi')->__("Google shopping only supports %d tax rates per product", self::RATES_MAX));
             }
             foreach ($regions as $region) {
                 $taxes[] = array('country' => empty($rate['country']) ? '*' : $rate['country'], 'region' => $region, 'rate' => $rate['value'] * 100, 'taxShip' => true);
             }
         }
     }
     $shoppingProduct->setTaxes($taxes);
     return $shoppingProduct;
 }
 /**
  * Set current attribute to entry (for specified product)
  *
  * @param Mage_Catalog_Model_Product $product
  * @param Google_Service_ShoppingContent_Product $shoppingProduct
  * @return Google_Service_ShoppingContent_Product
  */
 public function convertAttribute($product, $shoppingProduct)
 {
     // get category from product attribute
     $value = $product->getResource()->getAttribute('google_shopping_category')->getFrontend()->getValue($product);
     $value = preg_replace('/\\d+ /', '', $value);
     $shoppingProduct->setGoogleProductCategory($value);
     return $shoppingProduct;
 }
 /**
  * Set current attribute to entry (for specified product)
  *
  * @param Mage_Catalog_Model_Product $product
  * @param Google_Service_ShoppingContent_Product $shoppingProduct
  * @return Google_Service_ShoppingContent_Product
  */
 public function convertAttribute($product, $shoppingProduct)
 {
     $value = $this->_googleAvailabilityMap[(int) $product->isSalable()];
     if ($product->getTypeId() == "configurable") {
         $value = $this->_googleAvailabilityMap[1];
     }
     return $shoppingProduct->setAvailability($value);
 }
 /**
  * Set current attribute to entry (for specified product)
  *
  * @param Mage_Catalog_Model_Product $product
  * @param Google_Service_ShoppingContent_Product $shoppingProduct
  * @return Google_Service_ShoppingContent_Product
  */
 public function convertAttribute($product, $shoppingProduct)
 {
     $availableConditions = array(self::CONDITION_NEW, self::CONDITION_USED, self::CONDITION_REFURBISHED);
     $mapValue = $this->getProductAttributeValue($product);
     if (!is_null($mapValue) && in_array($mapValue, $availableConditions)) {
         $condition = $mapValue;
     } else {
         $condition = self::CONDITION_NEW;
     }
     return $shoppingProduct->setCondition($condition);
 }
 /**
  * Set current attribute to entry (for specified product)
  *
  * @param Mage_Catalog_Model_Product $product
  * @param Google_Service_ShoppingContent_Product $shoppingProduct
  * @return Google_Service_ShoppingContent_Product
  */
 public function convertAttribute($product, $shoppingProduct)
 {
     if (is_null($this->getName())) {
         return $shoppingProduct;
     }
     $productAttribute = Mage::helper('googleshoppingapi/product')->getProductAttribute($product, $this->getAttributeId());
     $type = $this->getGcontentAttributeType($productAttribute);
     $value = $this->getProductAttributeValue($product);
     if (!is_null($value)) {
         $name = Google_Utils::camelCase($this->getName());
         $shoppingProduct->offsetSet($name, $value);
     }
     return $shoppingProduct;
 }
 /**
  * Set current attribute to entry (for specified product)
  *
  * @param Mage_Catalog_Model_Product $product
  * @param Google_Service_ShoppingContent_Product $shoppingProduct
  * @return Google_Service_ShoppingContent_Product
  */
 public function convertAttribute($product, $shoppingProduct)
 {
     $url = $product->getGoogleShoppingImage();
     if ($url && $url != "no_selection") {
         $url = Mage::getModel('catalog/product_media_config')->getMediaUrl($product->getGoogleShoppingImage());
         $shoppingProduct->setImageLink($url);
         return $shoppingProduct;
     }
     $url = Mage::helper('catalog/product')->getImageUrl($product);
     if ($product->getImage() && $product->getImage() != 'no_selection' && $url) {
         $shoppingProduct->setImageLink($url);
     }
     return $shoppingProduct;
 }
Example #11
0
 /**
  * Set current attribute to entry (for specified product)
  *
  * @param Mage_Catalog_Model_Product $product
  * @param Google_Service_ShoppingContent_Product $shoppingProduct
  * @return Google_Service_ShoppingContent_Product
  */
 public function convertAttribute($product, $shoppingProduct)
 {
     $value = array();
     if ($product->getTypeId() == "configurable") {
         $associatedProducts = $product->getTypeInstance(true)->getAssociatedProducts($product);
         foreach ($associatedProducts as $item) {
             $value[] = $this->getProductAttributeValue($item);
         }
     } else {
         $value[] = $this->getProductAttributeValue($product);
     }
     $shoppingProduct->setSizes(array($value));
     return $shoppingProduct;
 }
 /**
  * Set current attribute to entry (for specified product)
  *
  * @param Mage_Catalog_Model_Product $product
  * @param Google_Service_ShoppingContent_Product $shoppingProduct
  * @return Google_Service_ShoppingContent_Product
  */
 public function convertAttribute($product, $shoppingProduct)
 {
     $mapValue = $this->getProductAttributeValue($product);
     if (!$mapValue) {
         $weight = $this->getGroupAttributeWeight();
         $mapValue = $weight ? $weight->getProductAttributeValue($product) : null;
     }
     if ($mapValue) {
         $shippingWeight = new Google_Service_ShoppingContent_ProductShippingWeight();
         $shippingWeight->setValue($mapValue);
         $shippingWeight->setUnit(self::WEIGHT_UNIT);
         $shoppingProduct->setShippingWeight($shippingWeight);
     }
     return $shoppingProduct;
 }
Example #13
0
 /**
  * Set current attribute to entry (for specified product)
  *
  * @param Mage_Catalog_Model_Product $product
  * @param Google_Service_ShoppingContent_Product $shoppingProduct
  * @return Google_Service_ShoppingContent_Product
  */
 public function convertAttribute($product, $shoppingProduct)
 {
     $sizes = array();
     if ($product->getTypeId() == "configurable") {
         $associatedProducts = $product->getTypeInstance(true)->getAssociatedProducts($product);
         foreach ($associatedProducts as $item) {
             $sizes[] = $this->getProductAttributeValue($item);
         }
     } else {
         $value = $this->getProductAttributeValue($product);
         $sizes = explode(",", $value);
         $sizes = array_map('trim', $sizes);
     }
     return $shoppingProduct->setSizes($sizes);
 }
 /**
  * Set current attribute to entry (for specified product)
  *
  * @param Mage_Catalog_Model_Product $product
  * @param Google_Service_ShoppingContent_Product $shoppingProduct
  * @return Google_Service_ShoppingContent_Product
  */
 public function convertAttribute($product, $shoppingProduct)
 {
     $productCategories = $product->getCategoryIds();
     // TODO: set Default value for product_type attribute if product isn't assigned for any category
     $value = 'Shop';
     if (!empty($productCategories)) {
         $category = Mage::getModel('catalog/category')->load(array_shift($productCategories));
         $breadcrumbs = array();
         foreach ($category->getParentCategories() as $cat) {
             $breadcrumbs[] = $cat->getName();
         }
         $value = implode(' > ', $breadcrumbs);
         Mage::log($value);
     }
     $shoppingProduct->setProductType($value);
     return $shoppingProduct;
 }
Example #15
0
 /**
  * Set current attribute to entry (for specified product)
  *
  * @param Mage_Catalog_Model_Product $product
  * @param Google_Service_ShoppingContent_Product $shoppingProduct
  * @return Google_Service_ShoppingContent_Product
  */
 public function convertAttribute($product, $shoppingProduct)
 {
     $mapValue = $this->getProductAttributeValue($product);
     $name = $this->getGroupAttributeName();
     if (!is_null($name)) {
         $mapValue = $name->getProductAttributeValue($product);
     }
     if (!is_null($mapValue)) {
         $titleText = $mapValue;
     } elseif ($product->getName()) {
         $titleText = $product->getName();
     } else {
         $titleText = 'no title';
     }
     $titleText = Mage::helper('googleshoppingapi')->cleanAtomAttribute($titleText);
     $shoppingProduct->setTitle($titleText);
     return $shoppingProduct;
 }
 /**
  * Set current attribute to entry (for specified product)
  *
  * @param Mage_Catalog_Model_Product $product
  * @param Google_Service_ShoppingContent_Product $shoppingProduct
  * @return Google_Service_ShoppingContent_Product
  */
 public function convertAttribute($product, $shoppingProduct)
 {
     $productCategories = $product->getCategoryIds();
     $value = 'Shop';
     if (!empty($productCategories)) {
         $category = Mage::getModel('catalog/category')->load(array_shift($productCategories));
         $breadcrumbs = array();
         foreach ($category->getParentCategories() as $cat) {
             $breadcrumbs[] = $cat->getName();
         }
         $value = implode(' > ', $breadcrumbs);
         if (!count($breadcrumbs)) {
             $value = $category->getName();
         }
     }
     $shoppingProduct->setProductType($value);
     return $shoppingProduct;
 }
Example #17
0
 /**
  * Set current attribute to entry (for specified product)
  *
  * @param Mage_Catalog_Model_Product $product
  * @param Google_Service_ShoppingContent_Product $shoppingProduct
  * @return Google_Service_ShoppingContent_Product
  */
 public function convertAttribute($product, $shoppingProduct)
 {
     $parentId = Mage::getModel('catalog/product_type_configurable')->getParentIdsByChild($product->getId());
     $parentItem = Mage::getModel('catalog/product')->load($parentId);
     $cCatId = array_reverse($childItem->getCategoryIds());
     $pCatId = array_reverse($parentItem->getCategoryIds());
     $cCat = Mage::getModel('catalog/category')->load($cCatId[0]);
     $pCat = Mage::getModel('catalog/category')->load($pCatId[0]);
     $baseUrl = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB);
     if (!Mage::getStoreConfig('web/seo/use_rewrites')) {
         if ($product->getTypeId() == 'simple' && $parentItem->getTypeId() == 'configurable' || 'bundle' || 'grouped') {
             $url = $parentItem->getProductUrl(false);
         } else {
             $url = $childItem->getProductUrl(false);
         }
     } else {
         if ($product->getTypeId() == 'simple' && $parentItem->getTypeId() == 'configurable' || 'bundle' || 'grouped') {
             $url = "{$baseUrl}{$parentItem->getUrlPath($pCat)}";
         } else {
             $url = "{$baseUrl}{$product->getUrlPath($cCat)}";
         }
     }
     if ($url) {
         $config = Mage::getSingleton('googleshoppingapi/config');
         if (!Mage::getStoreConfigFlag('web/url/use_store') && $config->getAddStoreCodeToUrl()) {
             Mage::log($config->getAddStoreCodeToUrl() ? "true" : "false");
             $urlInfo = parse_url($url);
             $store = $product->getStore()->getCode();
             if (isset($urlInfo['query']) && $urlInfo['query'] != '') {
                 $url .= '&___store=' . $store;
             } else {
                 $url .= '?___store=' . $store;
             }
         }
         if ($config->getAddUtmSrcGshopping($product->getStoreId())) {
             $url .= '&utm_source=GoogleShopping';
         }
         if ($customUrlParameters = $config->getCustomUrlParameters($product->getStoreId())) {
             $url .= $customUrlParameters;
         }
         $shoppingProduct->setLink($url);
     }
     return $shoppingProduct;
 }
Example #18
0
 /**
  * Set current attribute to entry (for specified product)
  *
  * @param Mage_Catalog_Model_Product $product
  * @param Google_Service_ShoppingContent_Product $shoppingProduct
  * @return Google_Service_ShoppingContent_Product
  */
 public function convertAttribute($product, $shoppingProduct)
 {
     $parentId = Mage::getModel('catalog/product_type_configurable')->getParentIdsByChild($product->getId());
     $parentItem = Mage::getModel('catalog/product')->load($parentId);
     $category = Mage::getModel('catalog/category')->load();
     if (!Mage::getStoreConfig('web/seo/use_rewrites')) {
         if ($product->getTypeId() == 'simple' && $parentItem->getTypeId() == 'configurable' || 'bundle' || 'grouped') {
             $url = $parentItem->getProductUrl(false);
         } else {
             $url = $product->getProductUrl(false);
         }
     } else {
         if ($product->getTypeId() == 'simple' && $parentItem->getTypeId() == 'configurable' || 'bundle' || 'grouped') {
             $url = sprintf('%s%s', Mage::getBaseUrl(), $parentItem->getUrlPath($category));
         } else {
             $url = sprintf('%s%s', Mage::getBaseUrl(), $product->getUrlPath($category));
         }
     }
     if ($url) {
         if (!Mage::getStoreConfigFlag('web/url/use_store')) {
             $urlInfo = parse_url($url);
             $store = $product->getStore()->getCode();
             if (isset($urlInfo['query']) && $urlInfo['query'] != '') {
                 $url .= '&___store=' . $store;
             } else {
                 $url .= '?___store=' . $store;
             }
         }
         $config = Mage::getSingleton('googleshoppingapi/config');
         if ($config->getAddUtmSrcGshopping($product->getStoreId())) {
             $url .= '&utm_source=GoogleShopping';
         }
         if ($customUrlParameters = $config->getCustomUrlParameters($product->getStoreId())) {
             $url .= $customUrlParameters;
         }
         $shoppingProduct->setLink($url);
     }
     return $shoppingProduct;
 }
Example #19
0
 /**
  * Set current attribute to entry (for specified product)
  *
  * @param Mage_Catalog_Model_Product $product
  * @param Google_Service_ShoppingContent_Product $shoppingProduct
  * @return Google_Service_ShoppingContent_Product
  */
 public function convertAttribute($product, $shoppingProduct)
 {
     $mapValue = $this->getProductAttributeValue($product);
     $description = $this->getGroupAttributeDescription();
     if (!is_null($description)) {
         $mapValue = $description->getProductAttributeValue($product);
     }
     if (!is_null($mapValue)) {
         $descrText = $mapValue;
     } elseif ($product->getDescription()) {
         $descrText = $product->getDescription();
     } else {
         $descrText = 'no description';
     }
     $processor = Mage::helper('cms')->getBlockTemplateProcessor();
     $descrText = strip_tags($processor->filter($descrText));
     $descrText = Mage::helper('googleshoppingapi')->cleanAtomAttribute($descrText);
     $descrText = html_entity_decode($descrText, null, "UTF-8");
     //$descrText = mb_convert_encoding($descrText,"UTF-8");
     $shoppingProduct->setDescription($descrText);
     return $shoppingProduct;
 }
 /**
  * Set current attribute to entry (for specified product)
  *
  * @param Mage_Catalog_Model_Product $product
  * @param Google_Service_ShoppingContent_Product $shoppingProduct
  * @return Google_Service_ShoppingContent_Product
  */
 public function convertAttribute($product, $shoppingProduct)
 {
     $value = Mage::getSingleton('googleshoppingapi/config')->getTargetCountry($product->getStoreId());
     $shoppingProduct->setTargetCountry($value);
 }
Example #21
0
/**
 * Build a Google Shopping Content Product from a WC Product
 * @param WC_Product $wc_product
 * @return Google_Service_ShoppingContent_Product
 */
function woogle_build_product($wc_product)
{
    // Create Google Shopping Content Product
    require_once plugin_dir_path(woogle_get_plugin_file()) . 'vendor/google-api-php-client/src/Google/Service/ShoppingContent.php';
    $product = new Google_Service_ShoppingContent_Product();
    // Custom Attributes
    $product_custom_attributes = array();
    // Product identifiers
    $sku = $wc_product->get_sku();
    if (empty($sku)) {
        if ($wc_product->is_type('variation')) {
            $product->setOfferId($wc_product->variation_id);
        } else {
            $product->setOfferId($wc_product->id);
        }
    } else {
        $product->setOfferId($sku);
    }
    if ($wc_product->is_type('variation')) {
        $product->setItemGroupId($wc_product->parent->id);
    }
    $woocommerce_id_attribute = new Google_Service_ShoppingContent_ProductCustomAttribute();
    $woocommerce_id_attribute->setName('woocommerce_id');
    $woocommerce_id_attribute->setValue($wc_product->id);
    $woocommerce_id_attribute->setType('int');
    $product_custom_attributes[] = $woocommerce_id_attribute;
    // Title
    $woogle_title = get_post_meta($wc_product->id, '_woogle_title', true);
    if (!empty($woogle_title)) {
        $product->setTitle(html_entity_decode(strip_tags($woogle_title)));
    } else {
        $product->setTitle(html_entity_decode(strip_tags($wc_product->get_title())));
    }
    // Description
    $woogle_description = get_post_meta($wc_product->id, '_woogle_description', true);
    if (!empty($woogle_description)) {
        $product->setDescription(html_entity_decode(strip_tags($woogle_description)));
    } else {
        $product->setDescription(html_entity_decode(strip_tags($wc_product->post->post_content)));
    }
    $product->setLink($wc_product->get_permalink());
    $image = $wc_product->get_image();
    $post_thumbnail_id = get_post_thumbnail_id($wc_product->id);
    if ($post_thumbnail_id) {
        $image_src = wp_get_attachment_image_src($post_thumbnail_id);
        $product->setImageLink(@$image_src[0]);
    }
    $product->setContentLanguage(substr(get_locale(), 0, 2));
    $product->setTargetCountry(WC()->countries->get_base_country());
    $product->setChannel('online');
    $product->setAvailability($wc_product->is_in_stock() ? 'in stock' : 'out of stock');
    // Condition
    $condition = get_post_meta($wc_product->id, '_woogle_condition', true);
    if (!empty($condition)) {
        $product->setCondition($condition);
    } else {
        $product->setcondition('new');
    }
    // Category
    $category = get_post_meta($wc_product->id, '_woogle_category', true);
    if (!empty($category)) {
        $product->setGoogleProductCategory($category);
    }
    // Brand
    $brand = get_post_meta($wc_product->id, '_woogle_brand', true);
    if (!empty($brand)) {
        $product->setBrand($brand);
    }
    // GTIN
    $gtin = get_post_meta($wc_product->id, '_woogle_gtin', true);
    if (!empty($gtin)) {
        $product->setGtin($gtin);
    }
    // MPN
    $mpn = get_post_meta($wc_product->id, '_woogle_mpn', true);
    if (!empty($mpn)) {
        $product->setMpn($mpn);
    }
    if (empty($gtin) && empty($mpn)) {
        $product->setIdentifierExists(false);
    }
    // Price
    $price = new Google_Service_ShoppingContent_Price();
    $price->setValue($wc_product->get_regular_price());
    $price->setCurrency(get_woocommerce_currency());
    $product->setPrice($price);
    // Sale price
    $wc_sale_price = $wc_product->get_sale_price();
    if (!empty($wc_sale_price)) {
        $sale_price = new Google_Service_ShoppingContent_Price();
        $sale_price->setValue($wc_sale_price);
        $sale_price->setCurrency(get_woocommerce_currency());
        $product->setSalePrice($sale_price);
        $post_id = $wc_product->is_type('variation') ? $wc_product->variation_id : $wc_product->id;
        $sale_price_dates_from = get_post_meta($post_id, '_sale_price_dates_from', true);
        $sale_price_dates_to = get_post_meta($post_id, '_sale_price_dates_to', true);
        if (!empty($sale_price_dates_from) && !empty($sale_price_dates_to)) {
            $effective_date_start = date('c', intval($sale_price_dates_from));
            $effective_date_end = date('c', intval($sale_price_dates_to));
            $product->setSalePriceEffectiveDate("{$effective_date_start}/{$effective_date_end}");
        }
    }
    // Shipping fields
    // Shipping weight
    $wc_product_weight = $wc_product->get_weight();
    if (!empty($wc_product_weight)) {
        $shipping_weight = new Google_Service_ShoppingContent_ProductShippingWeight();
        $shipping_weight->setValue($wc_product_weight);
        $shipping_weight->setUnit(get_option('woocommerce_weight_unit', 'kg'));
        $product->setShippingWeight($shipping_weight);
    }
    // Shipping dimensions
    $wc_dimension_unit = get_option('woocommerce_dimension_unit', 'cm');
    $wc_product_length = $wc_product->get_length();
    if (!empty($wc_product_length)) {
        $dimension = new Google_Service_ShoppingContent_ProductShippingDimension();
        $dimension->setValue($wc_product_length);
        $dimension->setUnit($wc_dimension_unit);
        $product->setShippingLength($dimension);
    }
    $wc_product_width = $wc_product->get_width();
    if (!empty($wc_product_width)) {
        $dimension = new Google_Service_ShoppingContent_ProductShippingDimension();
        $dimension->setValue($wc_product_width);
        $dimension->setUnit($wc_dimension_unit);
        $product->setShippingWidth($dimension);
    }
    $wc_product_height = $wc_product->get_height();
    if (!empty($wc_product_height)) {
        $dimension = new Google_Service_ShoppingContent_ProductShippingDimension();
        $dimension->setValue($wc_product_height);
        $dimension->setUnit($wc_dimension_unit);
        $product->setShippingHeight($dimension);
    }
    // Attributes
    $color = null;
    $material = null;
    $pattern = null;
    $size = null;
    $age_group = null;
    $gender = null;
    $adult = null;
    if ($wc_product->is_type('variation')) {
        // Variable product
        $wc_variation_attributes = $wc_product->get_variation_attributes();
        foreach ($wc_variation_attributes as $name => $value) {
            if (!empty($value)) {
                $attribute = new Google_Service_ShoppingContent_ProductCustomAttribute();
                $attribute->setName($name);
                $attribute->setValue($value);
                $attribute->setType('text');
                $product_custom_attributes[] = $attribute;
                // Google attributes
                if (empty($color)) {
                    $color = woogle_maybe_get_attribute('color', $name, $value);
                }
                if (empty($material)) {
                    $material = woogle_maybe_get_attribute('material', $name, $value);
                }
                if (empty($pattern)) {
                    $pattern = woogle_maybe_get_attribute('pattern', $name, $value);
                }
                if (empty($size)) {
                    $size = woogle_maybe_get_attribute('size', $name, $value);
                }
                if (empty($age_group)) {
                    $age_group = woogle_maybe_get_attribute('age-group', $name, $value);
                }
                if (empty($gender)) {
                    $gender = woogle_maybe_get_attribute('gender', $name, $value);
                }
                if (empty($adult)) {
                    $adult = woogle_maybe_get_attribute('adult', $name, $value);
                }
            }
        }
    } else {
        // Simple product
        $wc_attributes = $wc_product->get_attributes();
        foreach ($wc_attributes as $name => $wc_attribute) {
            if ($wc_attribute['is_taxonomy']) {
                $term_values = array();
                $terms = wp_get_post_terms($wc_product->id, $name);
                foreach ($terms as $term) {
                    $term_values[] = $term->slug;
                }
                if (!empty($term_values)) {
                    $value = implode(',', $term_values);
                    $attribute = new Google_Service_ShoppingContent_ProductCustomAttribute();
                    $attribute->setName($name);
                    $attribute->setValue($value);
                    $attribute->setType('text');
                    $product_custom_attributes[] = $attribute;
                    // Google attributes
                    if (empty($color)) {
                        $color = woogle_maybe_get_attribute('color', $name, $value);
                    }
                    if (empty($material)) {
                        $material = woogle_maybe_get_attribute('material', $name, $value);
                    }
                    if (empty($pattern)) {
                        $pattern = woogle_maybe_get_attribute('pattern', $name, $value);
                    }
                    if (empty($size)) {
                        $size = woogle_maybe_get_attribute('size', $name, $value);
                    }
                    if (empty($age_group)) {
                        $age_group = woogle_maybe_get_attribute('age-group', $name, $value);
                    }
                    if (empty($gender)) {
                        $gender = woogle_maybe_get_attribute('gender', $name, $value);
                    }
                    if (empty($adult)) {
                        $adult = woogle_maybe_get_attribute('adult', $name, $value);
                    }
                }
            }
        }
    }
    /*
     * Physical properties
     */
    // Color
    if (empty($color)) {
        $color = get_post_meta($wc_product->id, '_woogle_color', true);
    }
    if (!empty($color)) {
        $product->setColor($color);
    }
    // Material
    if (empty($material)) {
        $material = get_post_meta($wc_product->id, '_woogle_material', true);
    }
    if (!empty($material)) {
        $product->setMaterial($material);
    }
    // Pattern
    if (empty($pattern)) {
        $pattern = get_post_meta($wc_product->id, '_woogle_pattern', true);
    }
    if (!empty($pattern)) {
        $product->setPattern($pattern);
    }
    // Size
    if (empty($size)) {
        $size = get_post_meta($wc_product->id, '_woogle_size', true);
    }
    if (!empty($size)) {
        $product->setSizes(explode(', ', $size));
        $product->setSizeSystem(WC()->countries->get_base_country());
    }
    /*
     * Target consumer
     */
    // Age Group
    if (empty($age_group)) {
        $age_group = get_post_meta($wc_product->id, '_woogle_age_group', true);
    }
    if (!empty($age_group)) {
        $product->setAgeGroup($age_group);
    }
    // Gender
    if (empty($gender)) {
        $gender = get_post_meta($wc_product->id, '_woogle_gender', true);
    }
    if (!empty($gender)) {
        $product->setGender($gender);
    }
    // Gender
    if (empty($adult)) {
        $adult = get_post_meta($wc_product->id, '_woogle_adult', true);
    }
    if (!empty($adult) && $adult != 'no') {
        $product->setAdult(true);
    }
    $product->setCustomAttributes($product_custom_attributes);
    return $product;
}
 /**
  * @param Google_Service_ShoppingContent_Product $product
  * @param null|int                               $storeId
  *
  * @return Google_Service_ShoppingContent_Product
  */
 public function insertProduct(Google_Service_ShoppingContent_Product $product, $storeId = null)
 {
     $merchantId = $this->getConfig()->getConfigData('account_id', $storeId);
     $product->setChannel("online");
     $expDate = date("Y-m-d", time() + 30 * 24 * 60 * 60);
     //product expires in 30 days
     $product->setExpirationDate($expDate);
     $result = $this->getShoppingService($storeId)->products->insert($merchantId, $product);
     return $result;
 }
Example #23
0
 /**
  * Set current attribute to entry (for specified product)
  *
  * @param Mage_Catalog_Model_Product $product
  * @param Google_Service_ShoppingContent_Product $shoppingProduct
  * @return Google_Service_ShoppingContent_Product
  */
 public function convertAttribute($product, $shoppingProduct)
 {
     $value = Mage::helper('googleshoppingapi')->buildContentProductId($product->getId(), $product->getStoreId());
     return $shoppingProduct->setOfferId($value);
 }
Example #24
0
 /**
  * Set current attribute to entry (for specified product)
  *
  * @param Mage_Catalog_Model_Product $product
  * @param Google_Service_ShoppingContent_Product $shoppingProduct
  * @return Google_Service_ShoppingContent_Product
  */
 public function convertAttribute($product, $shoppingProduct)
 {
     $product->setWebsiteId(Mage::app()->getStore($product->getStoreId())->getWebsiteId());
     $product->setCustomerGroupId(Mage::getStoreConfig(Mage_Customer_Model_Group::XML_PATH_DEFAULT_ID, $product->getStoreId()));
     $store = Mage::app()->getStore($product->getStoreId());
     $targetCountry = Mage::getSingleton('googleshoppingapi/config')->getTargetCountry($product->getStoreId());
     $isSalePriceAllowed = true;
     //($targetCountry == 'US');
     // get tax settings
     $taxHelp = Mage::helper('tax');
     $priceDisplayType = $taxHelp->getPriceDisplayType($product->getStoreId());
     //$inclTax = ($priceDisplayType == Mage_Tax_Model_Config::DISPLAY_TYPE_INCLUDING_TAX);
     $inclTax = $priceDisplayType == Mage_Tax_Model_Config::DISPLAY_TYPE_INCLUDING_TAX || $priceDisplayType == Mage_Tax_Model_Config::DISPLAY_TYPE_BOTH;
     // calculate sale_price attribute value
     $salePriceAttribute = $this->getGroupAttributeSalePrice();
     $salePriceMapValue = null;
     $finalPrice = null;
     if (!is_null($salePriceAttribute)) {
         $salePriceMapValue = $salePriceAttribute->getProductAttributeValue($product);
     }
     if (!is_null($salePriceMapValue) && floatval($salePriceMapValue) > 0.0001) {
         $finalPrice = $salePriceMapValue;
     } else {
         if ($isSalePriceAllowed) {
             $finalPrice = Mage::helper('googleshoppingapi/price')->getCatalogPrice($product, $store, $inclTax);
         }
     }
     if ($product->getTypeId() != Mage_Catalog_Model_Product_Type::TYPE_BUNDLE) {
         $finalPrice = $taxHelp->getPrice($product, $finalPrice, $inclTax, null, null, null, $product->getStoreId());
     }
     // calculate price attribute value
     $priceMapValue = $this->getProductAttributeValue($product);
     $price = null;
     if (!is_null($priceMapValue) && floatval($priceMapValue) > 0.0001) {
         $price = $priceMapValue;
     } else {
         if ($isSalePriceAllowed) {
             $price = Mage::helper('googleshoppingapi/price')->getCatalogRegularPrice($product, $store);
         } else {
             $inclTax = $priceDisplayType != Mage_Tax_Model_Config::DISPLAY_TYPE_EXCLUDING_TAX;
             $price = Mage::helper('googleshoppingapi/price')->getCatalogPrice($product, $store, $inclTax);
         }
     }
     if ($product->getTypeId() != Mage_Catalog_Model_Product_Type::TYPE_BUNDLE) {
         $price = $taxHelp->getPrice($product, $price, $inclTax, null, null, null, $product->getStoreId());
     }
     $shoppingPrice = new Google_Service_ShoppingContent_Price();
     $shoppingPrice->setCurrency($store->getDefaultCurrencyCode());
     //sprintf('%.2f', $store->roundPrice($price))
     if ($isSalePriceAllowed) {
         // set sale_price and effective dates for it
         if ($price && $price - $finalPrice > 0.0001) {
             $salesPrice = new Google_Service_ShoppingContent_Price();
             $salesPrice->setCurrency($store->getDefaultCurrencyCode());
             $shoppingPrice->setValue(sprintf('%.2f', $store->roundPrice($price)));
             $salesPrice->setValue($finalPrice);
             $shoppingProduct->setSalePrice($salesPrice);
             $effectiveDate = $this->getGroupAttributeSalePriceEffectiveDate();
             if (!is_null($effectiveDate)) {
                 $effectiveDate->setGroupAttributeSalePriceEffectiveDateFrom($this->getGroupAttributeSalePriceEffectiveDateFrom())->setGroupAttributeSalePriceEffectiveDateTo($this->getGroupAttributeSalePriceEffectiveDateTo())->convertAttribute($product, $shoppingProduct);
             }
         } else {
             $shoppingPrice->setValue(sprintf('%.2f', $store->roundPrice($finalPrice)));
         }
         //TODO
         // 2011-03-01T13:00-0800/2011-03-11T15:30-0800
         // salePriceEffectiveDate
         // calculate taxes
         $tax = $this->getGroupAttributeTax();
         if (!$inclTax && !is_null($tax)) {
             $tax->convertAttribute($product, $shoppingProduct);
         }
     } else {
         $shoppingPrice->setValue(sprintf('%.2f', $store->roundPrice($price)));
     }
     $shoppingProduct->setPrice($shoppingPrice);
     return $shoppingProduct;
 }
 private function createExampleProduct($offer_id)
 {
     $product = new Google_Service_ShoppingContent_Product();
     $product->setOfferId($offer_id);
     $product->setTitle('A Tale of Two Cities');
     $product->setDescription('A classic novel about the French Revolution');
     $product->setLink('http://my-book-shop.com/tale-of-two-cities.html');
     $product->setImageLink('http://my-book-shop.com/tale-of-two-cities.jpg');
     $product->setContentLanguage('en');
     $product->setTargetCountry('US');
     $product->setChannel('online');
     $product->setAvailability('in stock');
     $product->setCondition('new');
     $product->setGoogleProductCategory('Media > Books');
     $product->setGtin('9780007350896');
     $price = new Google_Service_ShoppingContent_Price();
     $price->setValue('2.50');
     $price->setCurrency('USD');
     $product->setPrice($price);
     $shipping_price = new Google_Service_ShoppingContent_Price();
     $shipping_price->setValue('0.99');
     $shipping_price->setCurrency('USD');
     $shipping = new Google_Service_ShoppingContent_ProductShipping();
     $shipping->setPrice($shipping_price);
     $shipping->setCountry('US');
     $shipping->setService('Standard shipping');
     $product->setShipping(array($shipping));
     $shipping_weight = new Google_Service_ShoppingContent_ProductShippingWeight();
     $shipping_weight->setValue(200);
     $shipping_weight->setUnit('grams');
     $product->setShippingWeight($shipping_weight);
     return $product;
 }