Magento coupon discount - php

I am setting up magento store which buy stuff from customers. Instead of decreasing Price of the product, I want to increase the price of the product using coupon code. in which file i need to make changes for this.
Update 1:
Thanks Amit.
I have another question. I like to change "Discount" to "Promotion" in cart and onepage checkout. However, I can't find any file location. I have turn on the Template Path Hints from Configuration. Can anyone help me out ?

If you need only fixed amount discount, then you can remove the validate for Discount Amount field so that you can add negative value in this field, so when you try to apply this coupon it will automatically add that amount instead to decrease. So you need to override below two classes.
For more details on Magento override see this Link.
Mage_Adminhtml_Block_Promo_Quote_Edit_Tab_Actions
find this code
$fieldset->addField('discount_amount', 'text', array(
'name' => 'discount_amount',
'required' => true,
'class' => 'validate-not-negative-number',
'label' => Mage::helper('salesrule')->__('Discount Amount'),
));
and change it to
$fieldset->addField('discount_amount', 'text', array(
'name' => 'discount_amount',
'required' => true,
'label' => Mage::helper('salesrule')->__('Discount Amount'),
));
and remove the below code
if ($this->hasDiscountAmount()) {
if ((int)$this->getDiscountAmount() < 0) {
Mage::throwException(Mage::helper('rule')->__('Invalid discount amount.'));
}
}
from
Mage_Rule_Model_Abstract::_beforeSave()

Have a look at the CartController.php in Mage/Checkout/Controllers and at the Mage_Sales_Model_Quote model with its methode -collectTotals().
You need to create a new module where you override the model or create an observer.

Related

WooCommerce custom child products to variable products

I have some variable products (different 3-4 variations) with different prices. I want to add some simple products as child products (show as optional checkbox in front). I don't know how to manage that for adding to the cart.
For example let's assume I have a cellphone as main product and when user added it to it's cart it should see 3 sub product (child product) :
Handsfree (in three type of color)
Glass guard (in two type of color )
cell phone shield case ( just black)
as you can understand from above example I wanna to:
add all of this items as one product (includes handsfree, glass guard , cellphone case and cellphone itself ) and calculate it's price .I've read Add a checkbox on single product pages that adds an additional cost in Woocommerce and after that I have codes below to adding options in product edit page.
//custom product tab to woocommerce edit
function sm_wccpd_custom_product_tabs($tabs)
{
$tabs['sm_wcpa_extra_products'] = array(
'label' => __('Samanik Extra Products', 'sm-wcpa'),
'target' => 'sm_wc_extra_products_options',
'class' => array('show_if_variable'),
);
return $tabs;
}
add_filter('woocommerce_product_data_tabs', 'sm_wccpd_custom_product_tabs');
and here is tab content
function extra_products_options_product_tab_content()
{
global $post;
// Note the 'id' attribute needs to match the 'target' parameter set above
?>
<div id='sm_wc_extra_products_options' class='panel woocommerce_options_panel'><?php
?>
<div class='options_group'><?php
woocommerce_wp_checkbox(
[
'id' => '_has_extra_products',
'label' => __('has extra products', 'sm-wcpa'),
]);
woocommerce_wp_select(
[
'class' => 'multiselect attribute_values wc-enhanced-select',
'custom_attributes' => ['multiple' => 'multiple', 'style' => 'width:100% !importan;'],
'id' => '_sm_wcpa_product[]',
'label' => __('Extra Products to be add here', 'sm-wcpa'),
'value' => array_values(json_decode(get_post_meta($post->ID, '_sm_wcpa_product'), true)),
'options' => sm_wcpa_get_products_as_options(),//this is function that I pass Products as array like [product_id => product name]
]);
?></div>
</div><?php
}
// add_filter('woocommerce_product_data_tabs', 'extra_products_options_product_tab_content'); // WC 2.5 and below
add_filter('woocommerce_product_data_panels', 'extra_products_options_product_tab_content'); // WC 2.6 and up
also I can save the data and I think they are not necessary here.
now what I need to do is how to manage this in front. I mean show child products as checkbox, add price to parent price if checked, what about changing variables(those have different prices).
thanks for your patient to read my question.

Customizing checkout Postcode field into a custom drop-down menu

I am building a WooCommerce based store. I have a list of postcodes, each one has a different shipping cost attached through Shipping Zones (some provide free shipping, some have a flat rate).
When the customer goes to the checkout page, he needs to type his postcode number in the input field. Depending on postcode, an order preview will show different shipping total (free or flat rate).
Here's how the input field looks like in class-wc-countries.php:
public function get_default_address_fields() {
$fields = array(
'postcode' => array(
'label' => __( 'Postcode/ZIP', 'woocommerce' ),
'required' => true,
'class' => array( 'form-row-first', 'address-field' ),
'clear' => true,
'validate' => array( 'postcode' ),
'autocomplete' => 'postal-code',
),
);
However, what I want to do is to turn this field into a drop-down menu, so the customer could just select his postcode option rather than type it.
I managed to make it drop-down, but whenever I choose any option it doesn't seem to change shipping total as it would with input field.
Here's what I did:
public function get_default_address_fields() {
$fields = array(
'postcode' => array(
'label' => __( 'Postcode/ZIP', 'woocommerce' ),
'required' => true,
'class' => array( 'form-row-first', 'address-field' ),
'clear' => true,
'validate' => array( 'postcode' ),
'autocomplete' => 'postal-code',
'type' => 'select',
'options' => array(
'opt1' => "001122", "112200", "334400")
),
);
But this don't work.
Am I missing something?
How do I make these drop-down options change shipping total?
Thanks
This will answer very partially to your question, and just show you the way to customize checkout fields.
Overriding core files is not really something to do, as you will loose everithing each time Woocommerce is going to be updated and is not recommended.
To override checkout fields in a clean way, first you need to use a custom function hooked in one of that 2 filter hooks:
woocommerce_default_address_fields (when customizing billing and shipping address default fields)
woocommerce_checkout_fields (when customizing billing or shipping address fields and also others fields).
Related official documentation: Customizing checkout fields using actions and filters
So here I have chose the first hook, and I have corrected your post codes array. You will get that:
Here is that functional and tested code:
add_filter( 'woocommerce_default_address_fields' , 'custom_override_default_postcode_field' );
function custom_override_default_postcode_field( $address_fields ) {
// Your postcodes array
$postcode_array = array(
'opt1' => "001122",
'opt2' => "112200",
'opt3' => "334400"
);
$address_fields['postcode']['type'] = 'select';
$address_fields['postcode']['options'] = $postcode_array;
return $address_fields;
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
As selecting a post-code is a live front-end event, you will need to use a complex javascript/ajax script with some remote php function to achieve what you want to do, and this is a real development... It also depends on your settings and is complex to handle as there is already some woocommerce ajax scripts operating in that checkout page.

How to show newsletter subscription date on grid?

I want to show newsletter subscription date on Newsletter Subscribers Grid.
I am using magento 1.8.1.
How it will be possible.
Please suggest?
Thanks
By default, There isn't a subscription date stored when the customer subscribes, you will have to:
Add a subscription_date column to the newsletter_subscriber table
Override Mage_Newsletter_Model_Subscriber and in subscribe function, add code to set the subscription date to the model before saving $this->setSubscriptionDate(yourDateGoesHere)
Then, in order to show it int the backend, you will have to override Mage_Adminhtml_Block_Newsletter_Subscriber_Grid and add this code in _prepareColumns function, just place it in the place you want it to show.
$this->addColumn('subscription_date', array(
'header' => Mage::helper('newsletter')->__('Date'),
'index' => 'subscription_date',
'type' => 'datetime',
'align' => 'center',
'gmtoffset' => true
));

Show product on main page by id opencart

I need to show one certain product on front page. I didn't find a module for this. What do I need to do? I need to show product with countdown timer but I can do this, I only do not know how to get product from database by ID or SKU. I guess the code should be like this:
echo $product_option_data[] = array(
'product_option_id' => $product_option['product_option_id'],
'option_id' => $product_option['option_id'],
'name' => $product_option['name'],
'type' => $product_option['type'],
'option_value' => $product_option['option_value'],
'required' => $product_option['required']
);
In OpenCart controller you can call
$this->load->model('catalog/product'); // only if not yet 'loaded' within the controller
$product = $this->model_catalog_product->getProduct($productId);
print_r($product);
which will return you the product by it's ID. Do with it whatever you need afterwards.

Magento 'Select Configurable Attributes' with PHP

I have created new configurable products, and attached their simple products with PHP.
Now when I edit any configurable product I see this screen:
So in absence of any Magento documentation, what do I call in PHP to perform the same function as the screen above programatically?
I have seen $configurable_product->setConfigurableProductsData() used in some examples, but don't think it is what I need.
You are right, you are creating the association/link between configurable and child products but what's happening is that when you are creating your configurable product you are not setting up the setConfigurableAttributesData that basically setups the superattribute information for that configurable product.
foreach($configAttrCodes as $attrCode){
$super_attribute= Mage::getModel('eav/entity_attribute')->loadByCode('catalog_product',$attrCode->code);
$configurableAtt = Mage::getModel('catalog/product_type_configurable_attribute')->setProductAttribute($super_attribute);
$newAttributes[] = array(
'id' => $configurableAtt->getId(),
'label' => $configurableAtt->getLabel(),
'position' => $super_attribute->getPosition(),
'values' => $configurableAtt->getPrices() ? $configProduct->getPrices() : array(),
'attribute_id' => $super_attribute->getId(),
'attribute_code' => $super_attribute->getAttributeCode(),
'frontend_label' => $super_attribute->getFrontend()->getLabel(),
);
}
$existingAtt = $product->getTypeInstance()->getConfigurableAttributes();
if(empty($existingAtt) && !empty($newAttributes)){
$configProduct->setCanSaveConfigurableAttributes(true);
$configProduct->setConfigurableAttributesData($newAttributes);
$configProduct->save();
}
This is small snippet should get you there, let me know if you have any questions or need further help.

Categories